The following code causes a "Runaway argument" error in LaTeX after knitr has run:

\documentclass{article}

\begin{document}

<<fig.cap="This causes \\texttt{problems.}">>=
plot(1:10,10:1)
@

\end{document}

It's not obvious why, so I hope this helps others who may encounter this issue.

有帮助吗?

解决方案

The problem arises because, as stated in "Power of Graphics in knitr"

The default short caption is extracted from the caption by truncating it at the first period or colon or semi-colon.

Thus, the .tex file produced by knitting the above example contains the following

\caption[This causes \texttt{problems]{This causes \texttt{problems.}

The solution is to supply a short caption that is not truncated prematurely, e.g.,

\documentclass{article}

\begin{document}

<<fig.cap="This causes \\texttt{no problems.}", fig.scap="This causes \\texttt{no problems.}">>=
plot(1:10,10:1)
@

\end{document}

Many, many thanks to Yihui Xie for massively increasing my productivity and the reusability of my code!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top