
我正在嘗試使用 lstlisting 將 Maxima 程式碼嵌入到 LaTeX 中,並且在清單上方有額外的空白。這是我的序言中的內容:
\lstset{basicstyle=\ttfamily\color{blue},
breaklines=true,
breakatwhitespace=true,
boxpos=t,
resetmargins=true}
這是相關的 LaTeX 程式碼:
\begin{tabular}{ll}
\textcolor{red}{\ttfamily (\%i1)} &
\begin{lstlisting}
divide(x*((x^2*p^2-t^2)^2 + 2*x*p^3*(x-(2*x^3+2*x^2+x+1)*t)), (x+1)*(t^2-x^2*p^2)^2, t);
\end{lstlisting}
\end{tabular}
這是結果:
我希望列表的第一行與表格頂部對齊。
有人能建議如何解決這個問題嗎?
答案1
\documentclass
如果您能提供從到 為止的完整 MWE,那就太好了\end{document}
,這樣我們就不必猜測或使用水晶球。
儘管如此,我還是使用了球並稍微修改了你的例子。請不要因為任何錯誤的假設而責怪我。
您正在使用表格,在左列中顯示標記,在右列中顯示程式碼範例。您將程式碼範例包含在lstlisting
- 環境中,其行為與顯示的公式非常相似,也就是說,它在程式碼的頂部和底部添加了空格。
相反,您希望代碼與左列中的標籤對齊,對嗎?
要對齊程式碼,您必須使用命令\lstinline!code!
而不是上述環境。!
您可以選擇您想要顯示的代碼中不存在的任何其他字符,而不是使用感嘆號作為分隔符 ( )。
最後,你必須說服你的同桌,和平合作\lstinline
。我透過將第二列更改為定義寬度的列來做到這一點,並且能夠分割長線。就像一個普通的l
專欄一樣,它曾經是,我添加>{\raggedright\arraybackslash}
到它。這種新的列類型被命名為L
(大寫 L)。
這是我的例子:
\documentclass{article}
\usepackage{listings}
\usepackage{color}
%% New
\usepackage{array} % to define new column declarations.
%% Define a new column declaration
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\lstset{basicstyle=\ttfamily\color{blue},
breaklines=true,
breakatwhitespace=true,
boxpos=t,
resetmargins=true}
\begin{document}
\begin{tabular}{l L{5cm} } % <--- I used the new L-column, made it 5
% <--- cm wide!
\textcolor{red}{\ttfamily (\%i1)} &
%% New: use inline listing, it has to be one line!
\lstinline!divide(x*((x^2*p^2-t^2)^2 + 2*x*p^3*(x-(2*x^3+2*x^2+x+1)*t)), (x+1)*(t^2-x^2*p^2)^2, t);!
\end{tabular}
\end{document}
結果: