[R] Getting Rmarkdown to generate custom LaTEX environment

Duncan Murdoch murdoch.duncan at gmail.com
Wed Feb 25 20:07:11 CET 2015


On 25/02/2015 1:53 PM, Thierry Onkelinx wrote:
> This has nothing to do with *r*markdown but with the markdown language
> itself. Markdown has the concept of a figure caption. Hence fig.cap in
> Rmarkdown can work.

But rmarkdown is based on knitr, which is very flexible.  Presumably it 
is possible (and probably not that hard) to generate code to insert 
native LaTeX automatically.  It might look something like this, with the 
details filled in:

hook_fullpage <- function(before, options, envir) {
   # save the options
   # edit the options to set fig.env, which I think is a knitr concept
   # call the knitr latex hook
   # restore the options
}

knitr::knit_hooks$set(fullpage = hook_fullpage)

Then any chunk where you wanted this kind of inclusion you'd use the 
fullpage=TRUE option, and knitr
would insert a bit of LaTeX code into the middle of your markdown document.

Duncan Murdoch

> Op 25 feb. 2015 18:42 schreef "Huan Truong" <_ at tnhh.net>:
>
> > Hi Thierry,
> >
> > Thanks for the quick and informative answer. I understand that
> > rmarkdown doesn't know the concept of figure*. However, I wonder why
> > it knows that I could pass fig.cap fine, but not fig.env? Where in the
> > code of rmarkdown does it take care of that fig.cap handling, so I can
> > patch it to make it understand fig.env?
> >
> > - Huan.
> >
> > On Wed, Feb 25, 2015 at 3:39 AM, Thierry Onkelinx
> > <thierry.onkelinx at inbo.be> wrote:
> > > Dear Huan,
> > >
> > > Markdown doesn't know the concept of figure*. So you can't generate it
> > with
> > > native Markdown markup. If you really need figure*, then the only option
> > in
> > > markdown is to generate the LaTeX code yourself. Note that is will break
> > > conversion to formats that don't handle LaTeX code.
> > >
> > > Best regards,
> > >
> > > ir. Thierry Onkelinx
> > > Instituut voor natuur- en bosonderzoek / Research Institute for Nature
> > and
> > > Forest
> > > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> > > Kliniekstraat 25
> > > 1070 Anderlecht
> > > Belgium
> > >
> > > To call in the statistician after the experiment is done may be no more
> > than
> > > asking him to perform a post-mortem examination: he may be able to say
> > what
> > > the experiment died of. ~ Sir Ronald Aylmer Fisher
> > > The plural of anecdote is not data. ~ Roger Brinner
> > > The combination of some data and an aching desire for an answer does not
> > > ensure that a reasonable answer can be extracted from a given body of
> > data.
> > > ~ John Tukey
> > >
> > > 2015-02-25 3:28 GMT+01:00 Huan Truong <_ at tnhh.net>:
> > >>
> > >> Hi all,
> > >>
> > >> I was struggling trying to make my Rmarkdown document to generate
> > >>
> > >> \begin{figure*}
> > >>
> > >> instead of
> > >>
> > >> \begin{figure}
> > >>
> > >> So that the figure spans on two columns (I'm using a custom template).
> > >>
> > >> in my figure. I have read the Rmarkdown Reference Guide, and searched
> > >> for it on StackOverflow, and the general idea seems to be that I could
> > >> somehow pass the fig.env variable to the figure, but I have tried it
> > >> with no success: Something like this still doesn't have any effect
> > >> whatsoever on the latex code generated:
> > >>
> > >> ```{r test-plot, echo=FALSE, fig.cap = "Test
> > >> plot.\\label{fig:test-plot}", fig.env='figure*' }
> > >> options(fig.env='figure*')
> > >> plot(blah)
> > >> ```
> > >>
> > >> I know I must have missed something but couldn't figure out what I'm
> > >> missing after hours of struggling. Any help is much appreciated.
> > >>
> > >> I have a smaller problem where I want to customize the [htbp] option
> > >> of the figure, and facing the same problem.
> > >>
> > >> Cheers,
> > >> - Huan.
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >> --
> > >>
> > >> Eccentric Graduate Student
> > >> Google Talk/Jabber huant at tnhh.net - Website tnhh.net - Phone
> > >> 1-858-848-ROFL
> > >>
> > >> ______________________________________________
> > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >> https://stat.ethz.ch/mailman/listinfo/r-help
> > >> PLEASE do read the posting guide
> > >> http://www.R-project.org/posting-guide.html
> > >> and provide commented, minimal, self-contained, reproducible code.
> > >
> > >
> >
> >
> >
> > --
> >
> > Eccentric Graduate Student
> > Google Talk/Jabber huant at tnhh.net - Website tnhh.net - Phone
> > 1-858-848-ROFL
> >
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list