
Cuando intento superponer el lstlisting
código C con un carácter de escape (para poder insertar un \colorbox
), aparece el siguiente error:
! Missing number, treated as zero.
<to be read again>
\let
l.15 }
Consulte a continuación algún beamer
código que reproduzca el error. Tenga en cuenta que, si comento las instrucciones superpuestas (consulte la lectura de anotaciones % comment this
en mi código), el código se compila y funciona perfectamente.
Me gustaría que la superposición esté operativa con mi lstlisting
entorno. ¿Alguna pista sobre cómo resolver este problema?
\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}
Respuesta1
Aquí hay un código mínimo que reproduce los errores.
\documentclass{beamer}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]
\only<1>{
\begin{lstlisting}
\colorbox{blue}{x++;}
\end{lstlisting}
}
\end{frame}
\end{document}
Si elimina la macro superpuesta ( \only
), el código se compila bien. Esto le indica que el problema no tiene nada que ver con escapechar
. Más bien, el problema parece ser que \only
no aprecia el contenido textual (su lstlisting
entorno, en este caso).
Una solución alternativa es tener sus listados en archivos externos e insertarlos usando \lstinputlisting
; vea abajo.
Nota al margen: beamer
proporciona dos entornos columns
y column
, que son más idiomáticos que minipage
aquí. Usalos, usalos a ellos.
\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}
Respuesta2
Reemplazar \only<1>{...}
por \begin{onlyenv}<1>...\end{onlyenv}
para usar código textual dentro:
\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}