我可以有一个包含固定列代码和完全灵活注释的列表吗?

我可以有一个包含固定列代码和完全灵活注释的列表吗?

平均能量损失

\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@fullflexiblecommentstyle

因此,

\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} 

输出:

在此处输入图片描述

相关内容