iftoggle 与 minipage:错误缺少数字,非法计量单位

iftoggle 与 minipage:错误缺少数字,非法计量单位

同时结合切换小页面,我收到以下错误信息:

Missing number, treated as zero }
Illegal unit of measure (pt inserted) 
Emergency stop
Text dropped after begin of listing

\iftoggle与 lstlisting(无 minipage)结合使用效果很好。但我希望并排获得 2 个列表,C 和 Python 或 C++ 和 Python,具体取决于 toggletrue...

感谢帮助 !

\documentclass[12pt]{article} % classe de document
\usepackage[latin1]{inputenc} % encodage des caractères d'entrée
\usepackage[T1]{fontenc} % encodage de la police
\usepackage[english,frenchb]{babel}% langue

\usepackage{listings}

\usepackage{etoolbox}
\newtoggle{LANGC}
\toggletrue{LANGC}  

\begin{document}

\iftoggle{LANGC}{%
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=C,caption=code C,frame=tlrb]{Name}
int i = 1;
while (i <= 10) {
printf(" %d", i);
i = i + 1;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
i = 1
while (i <= 10):
print " ", i
i = i + 1
\end{lstlisting}
\end{minipage}
}{%
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=C++,caption=code C++,frame=tlrb]{Name}
int i = 1;
while (i <= 10) {
cout << i;
i = i + 1;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
i = 1
while (i <= 10):
print " ", i
i = i + 1
\end{lstlisting}
\end{minipage}
}         <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< error here !
\end{document}

感谢帮助 !

答案1

你不能lstlisting在另一个命令的参数中使用环境。很容易得到一个“独立”的形式:

\documentclass[12pt]{article} % classe de document
\usepackage[latin1]{inputenc} % encodage des caractères d'entrée
\usepackage[T1]{fontenc} % encodage de la police
\usepackage[english,frenchb]{babel}% langue

\usepackage{listings}

\usepackage{etoolbox}
\newtoggle{LANGC}
\toggletrue{LANGC}

\newcommand{\doiftoggle}[1]{\iftoggle{#1}{\iftrue}{\iffalse}\ignorespaces}

\begin{document}

\doiftoggle{LANGC}
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=C,caption=code C,frame=tlrb]{Name}
int i = 1;
while (i <= 10) {
printf(" %d", i);
i = i + 1;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
i = 1
while (i <= 10):
print " ", i
i = i + 1
\end{lstlisting}
\end{minipage}
\else
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=C++,caption=code C++,frame=tlrb]{Name}
int i = 1;
while (i <= 10) {
cout << i;
i = i + 1;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
i = 1
while (i <= 10):
print " ", i
i = i + 1
\end{lstlisting}
\end{minipage}
\fi

\togglefalse{LANGC}

\doiftoggle{LANGC}
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=C,caption=code C,frame=tlrb]{Name}
int i = 1;
while (i <= 10) {
printf(" %d", i);
i = i + 1;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
i = 1
while (i <= 10):
print " ", i
i = i + 1
\end{lstlisting}
\end{minipage}
\else
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=C++,caption=code C++,frame=tlrb]{Name}
int i = 1;
while (i <= 10) {
cout << i;
i = i + 1;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
i = 1
while (i <= 10):
print " ", i
i = i + 1
\end{lstlisting}
\end{minipage}
\fi

\end{document}

在此处输入图片描述

答案2

感谢您的评论,我会尝试如果结构(见底部...),但目前我改变了列表输入列表...导出源文件需要更多努力,但可以更轻松地检查它们

        \iftoggle{LANGC}{%
        \noindent\begin{minipage}{.45\textwidth}
        \lstinputlisting[language=C,caption={Compter de 1 à 10 en C},frame=tlrb]{code000.c}
        \end{minipage}\hfill
        \begin{minipage}{.45\textwidth}
        \lstinputlisting[language=Python,caption={Compter de 1 à 10 en Python},frame=tlrb]{code000.py}
        \end{minipage}
        }{%
        \noindent\begin{minipage}{.45\textwidth}
        \lstinputlisting[language=C++,caption={Compter de 1 à 10 en C++},frame=tlrb]{code000.c++}
        \end{minipage}\hfill
        \begin{minipage}{.45\textwidth}
        \lstinputlisting[language=Python,caption={Compter de 1 à 10 en Python},frame=tlrb]{code000.py}
        \end{minipage}
        }

甚至更短:

        \noindent\begin{minipage}{.45\textwidth}
        \iftoggle{LANGC}{%
        \lstinputlisting[language=C,caption={Compter de 1 à 10 en C},frame=tlrb]{code000.c}
        }{%
        \lstinputlisting[language=C++,caption={Compter de 1 à 10 en C++},frame=tlrb]{code000.c++}
        }
        \end{minipage}\hfill
        \begin{minipage}{.45\textwidth}
        \lstinputlisting[language=Python,caption={Compter de 1 à 10 en Python},frame=tlrb]{code000.py}
        \end{minipage}

编辑1

\如果... 工作正常 !

添加:

    \newif\iflangageC
    \langageCtrue

列表

        \noindent\begin{minipage}{.45\textwidth}
        \iflangageC
          \begin{lstlisting}[language=C,caption=code C,frame=tlrb]{Name}
          int i = 1;
          while (i <= 10) {
            printf(" %d", i);
            ++i;
          }
          \end{lstlisting}
        \else
          \begin{lstlisting}[language=C++,caption=code C++,frame=tlrb]{Name}
          int i = 1;
          while (i <= 10) {
            cout << " " << i;
            ++i;
           }
          \end{lstlisting}
        \fi
        \end{minipage}\hfill
        \begin{minipage}{.45\textwidth}
        \begin{lstlisting}[language=Python, caption=code Python ,frame=tlrb,firstnumber=1]{Name}
        i = 1
        while (i <= 10):
           print " ", i
           i = i + 1
         }
        \end{lstlisting}
        \end{minipage}

相关内容