使用 \only 和 knitr 会导致崩溃

使用 \only 和 knitr 会导致崩溃

似乎该\only命令不能用于显示R块。以下是产生错误的 MWE:

\documentclass[11pt]{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}

<<setup, include=FALSE>>=
library(knitr)
@

\title{Title}

\begin{document}
\section{Section name}
\begin{frame}[fragile]
\frametitle{\insertsection}

Add \verb!\only<1>{ ... }! around the code chunk to produce crash
<<>>=
2+2
@

\end{frame}

\end{document}

我收到此错误消息

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, : Running 'texi2dvi' on 'KnitrOnly.tex' failed. LaTeX errors: ! Paragraph ended before \@xverbatim was complete. <to be read again> \par l.16 } ! LaTeX Error: \begin{kframe} on input line 16 ended by \end{beamer@framepauses }. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ! Missing } inserted. <inserted text> } l.85 \end{frame}

! Extra }, or forgotten \endgroup. \endbeamer@frameslide ...amer@framepauses}\egroup \ifx \beamer@frametitle \@... l.85 \end{frame} ! LaTeX Error: \begin{knitrout} on input line 16 ended by \end{beamer@frameslid e}. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ! LaTeX Error: \begin{beamer@framepauses} on input line 85 ended by \end{docume nt}. See the LaTeX manual or LaTeX Companion

答案1

它与 一起工作\onslide<>,见图。

\onslide<2->
<<>>=
2+2
@
\onslide<3>
<<>>=
3+3
@

\onslide<> 在 knitr 中的工作示例

相关内容