
當我嘗試使用lstlisting
轉義字元覆蓋 C 程式碼(以便我可以插入\colorbox
)時,出現以下錯誤:
! Missing number, treated as zero.
<to be read again>
\let
l.15 }
請參閱下面的一些beamer
重現錯誤的程式碼。請注意,如果我註解掉覆蓋指令(請參閱% comment this
我的程式碼中的註解閱讀),則程式碼將編譯並完美運行。
我希望覆蓋層能夠在我的lstlisting
環境中運作。關於如何解決這個問題有任何線索嗎?
\documentclass[smaller]{beamer}
\usepackage{graphicx}
\usepackage{color,xcolor}
\usepackage{boxedminipage}
\usepackage{listings}
\usepackage{pgf,pgfpages}
\usepackage{tikz, subfig}
\usetikzlibrary{arrows,shapes,positioning}
\usetheme{boxes}
\usefonttheme[stillsansseriftext,stillsansserifsmall]{serif}
\setbeamerfont{frametitle}{size=\large,series=\bfseries,shape=\sf}
\definecolor{red} {rgb}{.8,0,0}
\definecolor{blue} {rgb}{0,0,.9}
\begin{document}
\begin{frame}[fragile]
\frametitle{C Program}
\only<1>{ % comment this
\begin{minipage}{\textwidth}
\begin{minipage}{0.5\textwidth}
\begin{lstlisting} [language=C,keywordstyle=\color{red},escapechar=\!,basicstyle=\ttfamily\scriptsize]
!\colorbox{blue}{x++;}!
\end{lstlisting}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\begin{lstlisting}[language=C,keywordstyle=\color{red},escapechar=\!, basicstyle=\ttfamily\scriptsize]
!\colorbox{red}{y++;}!
\end{lstlisting}
\end{minipage}
\end{minipage}
} % comment this
\only<2>{ % comment this
\begin{tikzpicture}[font=\footnotesize]
\node at (0,0) {$P_M$};
\node at (1,0) {$P_{W_1}$};
\node at (2,0) {$P_{W_2}$};
\draw (-1,-0.3) -- (3,-0.3);
\end{tikzpicture}
} % Comment this
\end{frame}
\end{document}
答案1
這是一些重現錯誤的最小程式碼。
\documentclass{beamer}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]
\only<1>{
\begin{lstlisting}
\colorbox{blue}{x++;}
\end{lstlisting}
}
\end{frame}
\end{document}
如果刪除覆蓋宏 ( \only
),程式碼可以正常編譯。這告訴您該問題與 無關escapechar
。相反,問題似乎在於\only
不喜歡逐字內容(lstlisting
在本例中是您的環境)。
一種解決方法是將您的清單放在外部文件中並使用\lstinputlisting
;插入它們。見下文。
附註:beamer
提供了兩個環境columns
和,這比這裡column
更慣用。minipage
使用它們。
\documentclass[smaller]{beamer}
\usepackage{graphicx}
\usepackage{color,xcolor}
\usepackage{boxedminipage}
\usepackage{listings}
\usepackage{pgf,pgfpages}
\usepackage{tikz, subfig}
\usetikzlibrary{arrows,shapes,positioning}
\usepackage{filecontents}
\usetheme{boxes}
\usefonttheme[stillsansseriftext,stillsansserifsmall]{serif}
\setbeamerfont{frametitle}{size=\large,series=\bfseries,shape=\sf}
\definecolor{red}{rgb}{.8,0,0}
\definecolor{blue}{rgb}{0,0,.9}
\begin{filecontents*}{samplex.c}
!\colorbox{blue}{x++;}!
\end{filecontents*}
\begin{filecontents*}{sampley.c}
!\colorbox{red}{y++;}!
\end{filecontents*}
\lstdefinestyle{myC}
{
language=C,
keywordstyle=\color{red},
escapechar=!,
basicstyle=\ttfamily\scriptsize,
}
\begin{document}
\begin{frame}[fragile]
\frametitle{C Program}
\only<1>{
\begin{columns}
\begin{column}{.5\textwidth}
\lstinputlisting[style=myC]{samplex.c}
\end{column}
\begin{column}{.5\textwidth}
\lstinputlisting[style=myC]{sampley.c}
\end{column}
\end{columns}
}
\only<2>{
\begin{tikzpicture}[font=\footnotesize]
\node at (0,0) {$P_M$};
\node at (1,0) {$P_{W_1}$};
\node at (2,0) {$P_{W_2}$};
\draw (-1,-0.3) -- (3,-0.3);
\end{tikzpicture}
}
\end{frame}
\end{document}
答案2
替換\only<1>{...}
為\begin{onlyenv}<1>...\end{onlyenv}
以使用內部逐字碼:
\documentclass[smaller]{beamer}
\usepackage{graphicx}
\usepackage{color,xcolor}
\usepackage{boxedminipage}
\usepackage{listings}
\usepackage{pgf,pgfpages}
\usepackage{tikz, subfig}
\usetikzlibrary{arrows,shapes,positioning}
\usetheme{boxes}
\usefonttheme[stillsansseriftext,stillsansserifsmall]{serif}
\setbeamerfont{frametitle}{size=\large,series=\bfseries,shape=\sf}
\definecolor{red} {rgb}{.8,0,0}
\definecolor{blue} {rgb}{0,0,.9}
\begin{document}
\begin{frame}[fragile]
\frametitle{C Program}
\begin{onlyenv}<1>
\begin{minipage}{\textwidth}
\begin{minipage}{0.5\textwidth}
\begin{lstlisting} [language=C,keywordstyle=\color{red},escapechar=\!,basicstyle=\ttfamily\scriptsize]
!\colorbox{blue}{x++;}!
\end{lstlisting}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\begin{lstlisting}[language=C,keywordstyle=\color{red},escapechar=\!, basicstyle=\ttfamily\scriptsize]
!\colorbox{red}{y++;}!
\end{lstlisting}
\end{minipage}
\end{minipage}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{tikzpicture}[font=\footnotesize]
\node at (0,0) {$P_M$};
\node at (1,0) {$P_{W_1}$};
\node at (2,0) {$P_{W_2}$};
\draw (-1,-0.3) -- (3,-0.3);
\end{tikzpicture}
\end{onlyenv}
\end{frame}
\end{document}