Skip to content

latex \subfloat{} incompatible with sweave/knitr code

10 messages · Steve Lianoglou, Yihui Xie, Duncan Mackay +1 more

#
Dear all
Are LaTeX \subfloat{} commands incompatible with Sweave code? I cannot
get the following code to compile properly:
\begin{table}
\subfloat[asdfa]{<<>>=
2+2
@

}

\caption{asdf}

\end{table}


If I replace the Sweave chunk with a random string or a table, the
compilation works fine. Any ideas what happens? I hit the same trouble
when running the code chunks through knitr.

Regards
Liviu
#
Hi,
On Wed, Aug 29, 2012 at 6:56 AM, Liviu Andronic <landronimirc at gmail.com> wrote:
This isn't exactly what you want, but I'm using kintr and building and
saving my figures in the their own "chunks" then just inlining the
path to the generated figure in the \subloat{..}. Things are working
fine, eg. my default settings are to suppress chunk echo/output,
generate pdf figures, and fig.path='figs/gen-' so:

<<someFig>>
plot(1:10, 1:10, ...)
@

\begin{figure}[...]
...
  \sublfoat[some][caption]{
    \includegraphics[...]{figs/gen-someFig.pdf}
  }
...
\end{figure}

does the trick for me.

HTH,
-steve
#
On Wed, Aug 29, 2012 at 1:34 PM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
Hmm, but how would I use this for tables? In the subfloats I'm
outputting Hmisc::describe() results.

Thanks
Liviu
#
Yes that is one possible solution, but the filename is hard-coded
somehow. The key to this problem is a missing new line before <<>>=,
which was addressed in
https://github.com/downloads/yihui/knitr/knitr-subfloats.pdf

The LyX and Rnw source files can be checked out with GIT under
https://github.com/yihui/knitr/tree/master/inst/examples If you open
knitr-subfloats.lyx, you will see an intentional new line in the ERT,
and that is to break \subfloat[asdfa]{<<>>= into

\subfloat[asdfa]{
<<>>=

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Wed, Aug 29, 2012 at 6:34 AM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
#
Hey Yihui
On Wed, Aug 29, 2012 at 6:17 PM, Yihui Xie <xie at yihui.name> wrote:
Thanks a lot for the example.
This trick doesn't seem to work for table subfloats. I added a nearly
identical example to your document, this time using table floats
(instead of figure floats). [1] The document doesn't compile. Any
ideas?

Regards
Liviu

[1] http://s000.tinyupload.com/index.php?file_id=64085332583398153478
#
Do you know what environments are allowed inside \subfloat{}? The
graphics example works because it is nothing but a simple
\includegraphics{} command. The table example you gave is much more
complicated than that.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA
On Wed, Aug 29, 2012 at 3:30 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
#
Hi Yihui

For figures in latex my bible is
Keith Reckdahl Using Imported Graphics in LATEX and pdfLATEX Version 
3.0.1 January 12, 2006
I think there is a 2009 version but I cannot find it

Below is an example of a 3 X 1 figure

\begin{figure}[h]
\centering
% Figure A
\subfloat[A]{
\label{subfig:continued:S13a} %% label ref sub A
\includegraphics[height=4in,%
                  clip=true,%
                  trim=0in 0in 0in 0in,% LBRT
                  keepaspectratio=true]%
                  {OV02a.pdf}
% caption
\caption[Regression diagnostic test plot: individual farms]%
{Regression diagnostic test plot: individual farms}%
\label{fig:continued:OV2a} %% label for first figure
\end{figure}
%
% Figure B
\begin{figure}[!h]
\ContinuedFloat
\centering
\subfloat[B]{
\label{subfig:continued:S13b} %% label ref subfig B
\includegraphics[height=4in,%
                  clip=true,%
                  trim=0in 0in 0in 0in,% LBRT
                  keepaspectratio=true]%
                  {OV02b.pdf}
% caption
\caption[Regression diagnostic test plot: individual farms]%
{Regression diagnostic test plot: individual farms }%
\label{fig:continued:OV2b} %% label for second figure
\end{figure}
%
% Figure C
\begin{figure}[!h]
\ContinuedFloat
\centering
\subfloat[C]{
\label{subfig:continued:S13c} %% label subfig C
\includegraphics[height=4in,%
                  clip=true,%
                  trim=0in 0in 0in 0in,% LBRT
                  keepaspectratio=true]%
                  {OV02c.pdf}
% caption
\caption[Regression diagnostic test plot: individual farms]%
{Regression diagnostic test plot for individual farms}%
\label{fig:continued:OV2c} %% label for 3 figure
\end{figure}
\clearpage

the code also works with sidewaysfigure
At first I had problems with getting the subcaptions right - I think 
this is right
In the subfig label I added the figure 'number' S13 to avoid 
duplicating if more than one in a tex file
There may be other variants but see     Reckdahl - you may have to 
change things slightly for spacing
I have amended it to omit \Sexpr values within

HTH

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
At 08:50 30/08/2012, you wrote:
#
On Thu, Aug 30, 2012 at 12:50 AM, Yihui Xie <xie at yihui.name> wrote:

  
    
  
1 day later
#
On Thu, Aug 30, 2012 at 12:50 AM, Yihui Xie <xie at yihui.name> wrote:
No, not really. From what I can tell, \subfloat{} is provided by the
`subfig' package. Here's what their docs have to say about
compatibility with verbatim and fancyvrb packages:
"You cannot place a verbatim environment inside of the \subfloat command because
the verbatim environment needs to change the character classes before
the text in the environment is read by TEX. Therefore, if you really
want to include verbatim text inside a sub-float, you will need to
place the verbatim text into a box and then feed the box to the
\subfloat command."

And this is what CTAN says about the 'subfig' package:
"The package 'subfigure' is now considered obsolete: it was superseded
by 'subfig', but users may find the more recent 'subcaption' package
more satisfactory."
I played around with the 'subcaption' package and, although it has
some side effects  on the figures, it accepts verbatim in its
'subtable' environment. See knitr LyX file and PDF:
http://s000.tinyupload.com/index.php?file_id=00455858466446731442
http://s000.tinyupload.com/index.php?file_id=07968719976440864387

Maybe it's high time to ask the LyX devels to implement support for
the 'subcaption' package.

Liviu
#
OK, then this turns out to be a pure LaTeX problem. Thanks for the
experiments and examples, and they worked very well for me. It seems
subcaption is on the radar of LyX 2.1 now.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA
On Fri, Aug 31, 2012 at 9:40 AM, Liviu Andronic <landronimirc at gmail.com> wrote: