
結合的同時如果切換和小型頁面,我收到以下錯誤訊息:
Missing number, treated as zero }
Illegal unit of measure (pt inserted)
Emergency stop
Text dropped after begin of listing
\iftoggle
與 lstlisting 結合使用效果很好(沒有小型頁面)。但我想並排獲得 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
感謝您的評論,我會嘗試如果結構(見底部...),但目前我改變了列表列表到lst輸入列表....需要付出更多努力來匯出原始文件,但有機會更輕鬆地檢查它們
\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}