# escaping whole HTML blocks from LaTeX export in book down

I am trying to do the following using rmarkdown and bookdown:

1. process a saved image as a text-wrap-around in HTML. I have a CSS definition that works perfectly:
<aside class="marginfigure">
<3 ticks>{r rabbit, fig.cap="Kuhn's favorite example.",  echo=FALSE,dev='png'}
knitr::include_graphics('./images/0_Introduction/Duck-Rabbit_illusion.jpg')
<3 ticks>
</aside>

1. Process that same block as text-wrap in the LaTeX export. I have a wrapfig setup in the preamble that works perfectly from bookdown as well:
\begin{wrapfigure}{R}{.45\textwidth}
\begin{center}
\includegraphics[width=.40\textwidth]{./images/0_Introduction/Duck-Rabbit_illusion.jpg}
\captionsetup{style=figures}
\caption{Kuhn's favorite example.}
\end{center}
\end{wrapfigure}


This works perfectly.

1. My goal is to have both processes aimed at both outputs in the same build and I'm prepared to repeat instructions in my source, like:
<!--.........................................-->

<aside class="marginfigure">
<3 ticks>{r rabbit, fig.cap="Kuhn's favorite example.",  echo=FALSE,dev='png'}
knitr::include_graphics('./images/0_Introduction/Duck-Rabbit_illusion.jpg')
<3 ticks>
</aside>

<!--.................-->

\begin{wrapfigure}{R}{.45\textwidth}
\begin{center}
\includegraphics[width=.40\textwidth]{./images/0_Introduction/Duck-Rabbit_illusion.jpg}
\captionsetup{style=figures}
\caption{Kuhn's favorite example.}
\end{center}
\end{wrapfigure}

<!--.........................................-->


If I do that, then the LaTeX "distillation" by pandoc from bookdown is properly ignored and the HTML result is perfect.

However, the pdflatex distillation by pandoc does not ignore the HTML stuff and takes the :include_graphics('./images/0_Introduction/Duck-Rabbit_illusion.jpg instruction from knitr as the LaTeX instruction to render that image...a second time, and of course, full-width.

I found that the pandoc exception:

<aside class="marginfigure">
<3 ticks>{r rabbit, fig.cap="Kuhn's favorite example.",  echo=FALSE,dev='png'}
knitr::include_graphics('./images/0_Introduction/Duck-Rabbit_illusion.jpg')
<3 ticks>
</aside>{=html}


Properly escapes the HTML code in the LaTeX distillation, but the figure number in the knitr outcome is then ignored and instead the figure caption comes out: (#fig:rabbit)Kuhn's favorite example.

The only way I can find to have the figure caption properly number the figure in the HTML result is to insert a blank line after

<aside class="marginfigure">

<3 ticks>{r rabbit, fig.cap="Kuhn's favorite example.",  echo=FALSE,dev='png'}
knitr::include_graphics('./images/0_Introduction/Duck-Rabbit_illusion.jpg')
<3 ticks>
</aside>{=html}


But this then makes LaTeX spit out some of the  {=html} characters and again give a second figure.

It would be great if it was possible to have tags that tell bookdown to ignore everything betwen the tags in the HTML or LaTeX processing.

Or, it would be great if ignoring the blank line could not break the figure numbering in the caption.

Am I missing something obvious?

I can't really help without a reproducible example, but maybe you can use the eval chunk option and the function knitr::is_html_output() to do what you want.

<aside class="marginfigure">
<3 ticks>{r rabbit, fig.cap="Kuhn's favorite example.",  echo=FALSE,dev='png', eval=knitr::is_html_output()}
knitr::include_graphics('./images/0_Introduction/Duck-Rabbit_illusion.jpg')
<3 ticks>
</aside>

1 Like

BIngo! That seems to fix it. I knew there had to be a way. Thanks for your quick response!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.