平均能量损失
\documentclass{article}
\usepackage{listings}
\usepackage{lmodern}
\lstset{
language=C,
basicstyle=\ttfamily,
commentstyle=\rmfamily
}
\begin{document}
\begin{lstlisting}
int a; // a number
int b; // another number
// ...
int sum = add(a, b); // their sum
\end{lstlisting}
\begin{lstlisting}[columns=fullflexible]
int a; // a number
int b; // another number
// ...
int sum = add(a, b); // their sum
\end{lstlisting}
\end{document}
我想将这两种风格融合在一起:
fixed
代码的列对齐(或任何内容)不是一条评论)fullflexible
评论的列对齐
答案1
看起来这是可能的,添加\lst@column@fullflexible
到commentstyle
。
因此,
\makeatletter
\let\commentfullflexible\lst@column@fullflexible
\makeatother
定义你的\lstset
为
\lstset{
language=C,
basicstyle=\ttfamily,
commentstyle=\rmfamily\commentfullflexible
}
梅威瑟:
\documentclass{article}
\usepackage{listings}
\usepackage{lmodern}
\makeatletter
\let\commentfullflexible\lst@column@fullflexible
\makeatother
\begin{document}
\lstset{
language=C,
basicstyle=\ttfamily,
commentstyle=\rmfamily
}
\begin{lstlisting}[caption={Normal comments}]
int a; // a number
int b; // another number
// ...
int sum = add(a, b); // their sum
\end{lstlisting}
\lstset{
language=C,
basicstyle=\ttfamily,
commentstyle=\rmfamily\commentfullflexible
}
\begin{lstlisting}[caption={Fullflexible comments}]
int a; // a number
int b; // another number
// ...
int sum = add(a, b); // their sum
\end{lstlisting}
\end{document}
输出: