我希望列表中的文本按照 MS Word 术语“左对齐”。
请参阅下面的代码片段。请注意“first”一词前后的空格量不同。我不想要“first”后面的额外空格。
下面是产生该结果的代码:
\documentclass[]{article}
\usepackage{listings}
\begin{document}
\begin{figure}[h!]
\lstset{
language=Python,
numberstyle=\tiny,
deletekeywords={from,in,and},
morekeywords={function,Algorithm,algorithm, then,do},
mathescape=true,
numbers=left,
xleftmargin=.04\textwidth,
breaklines=true,
flexiblecolumns=true
}
\begin{lstlisting}
Z = Y(from 1 to rand(1, Size(P(j))) # from the first till a random value
\end{lstlisting}
\caption{figure caption}
\label{fig1}
\end{figure}
\end{document}
答案1
您需要 columns=fullflexible
\documentclass[]{article}
\usepackage{listings}
\begin{document}
\begin{figure}[h!]
\lstset{
language=Python,
numberstyle=\tiny,
deletekeywords={from,in,and},
morekeywords={function,Algorithm,algorithm, then,do},
mathescape=true,
numbers=left,
xleftmargin=.04\textwidth,
breaklines=true,
columns=fullflexible,
flexiblecolumns=true,
}
\begin{lstlisting}
Z = Y(from 1 to rand(1, Size(P(j))) # from the first till a random value
\end{lstlisting}
\caption{figure caption}
\label{fig1}
\end{figure}
\end{document}