我目前在用 latex 编写 C++ 时遇到了问题。我的代码中有一串很长的文本,这会干扰空间。
那么我该如何解决这个问题呢?
代码:
\documentclass[11pt,a4paper,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
commentstyle=\color{green}, % comment color
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{red} % string color
}
\begin{lstlisting}[language=C++]
while(again == 1)
{ //again er defineret som 1, loekken koeres igen.
cout << "Velkommen til BMI beregneren!" << endl;
cout << "\nEr du en mand eller kvinde? Tast 1 for mand, tast 2 for kvinde" << endl;
cin >> sex;
cout << "Indtast din hojde i cm." << endl;
cin >> height;
cout << "Indtast din vaegt i KG." << endl;
cin >> weight;
\end{lstlisting}
答案1
breaklines
Jubobs 指出,该选项确实有效,且没有任何警告。您可以这样做:
\documentclass[11pt,a4paper,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
commentstyle=\color{green}, % comment color
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{red} % string color
}
\begin{document}
\begin{lstlisting}[language=C++,breaklines]
while(again == 1)
{ //again er defineret som 1, loekken koeres igen.
cout << "Velkommen til BMI beregneren!" << endl;
cout << "\nEr du en mand eller kvinde? Tast 1 for mand, tast 2 for kvinde" << endl;
cin >> sex;
cout << "Indtast din hojde i cm." << endl;
cin >> height;
cout << "Indtast din vaegt i KG." << endl;
cin >> weight;
\end{lstlisting}
\end{document}