我也有同样的问题: tex.stackexchange.com/questions/9510/changing-side-of-line-numbering-in-two-columns-documents/
我想listings
在文档中使用该包twocolumn
。类似这样的选项numbers=outside
很棒,但目前还不存在?
我尝试了以下操作:
\makeatletter
\def\@numpos\if\@firstcolumn left\else right\fi
\makeatother
并在\lstset
我设置numbers=\@numpos
。显然这样不行。
上述问题的第一个答案是一个巧妙的解决方案,但根本不能满足我和另一个问题的作者的要求。提出的解决方案也有一个缺陷:行号向右偏移(如果numbers=left
),如果向左偏移我会更喜欢。
顺便说一句/题外话:我评论了上述问题,但我的回答被删除了,我被建议开始我自己的问题。可能是因为我的回答不是真正的答案,只是另一个问题。
答案1
我为 定义了一个新选项numbers
,称为outside
。如果您在左列,它会在左边距打印数字;如果您在右列,它会在右边距打印数字。
这是相关代码:
\makeatletter
\lst@Key{numbers}{none}{%
\let\lst@PlaceNumber\@empty
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}}\\%
outside&\def\lst@PlaceNumber{\if@firstcolumn%
\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}%
\else%
\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}%
\fi%
}%
}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
\makeatother
这是一个 MWE:
\documentclass[twocolumn]{article}
\usepackage{listings}
\makeatletter
\lst@Key{numbers}{none}{%
\let\lst@PlaceNumber\@empty
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}}\\%
outside&\def\lst@PlaceNumber{\if@firstcolumn%
\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}%
\else%
\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}%
\fi%
}%
}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
\makeatother
\lstset{numbers=outside,frame=tb,numbersep=1em,language=pascal}
\begin{document}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\newpage
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\end{document}
得出的结果是:
编辑
以下内容适用于所有尺寸的标准类别,但可能需要根据您的\lstset
设置进行调整。
\newlength{\mylength}
\makeatletter
\lst@Key{numbers}{none}{%
\let\lst@PlaceNumber\@empty
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}}\\%
outside&\def\lst@PlaceNumber{%
\setlength{\mylength}{\dimexpr\textheight-\pagetotal-\baselineskip+6pt\relax}%
\if@firstcolumn%
\ifdim\mylength>0pt%
\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}%
\else%
\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}%
\fi%
\else%
\ifdim\mylength>0pt%
\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}%
\else%
\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}%
\fi%
\fi%
}%
}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
\makeatother
平均能量损失
\documentclass[twocolumn]{article}
\usepackage{listings}
\usepackage{lipsum} % only for the example
\newlength{\mylength}
\makeatletter
\lst@Key{numbers}{none}{%
\let\lst@PlaceNumber\@empty
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}}\\%
outside&\def\lst@PlaceNumber{%
\setlength{\mylength}{\dimexpr\textheight-\pagetotal-\baselineskip+6pt\relax}%
\if@firstcolumn%
\ifdim\mylength>0pt%
\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}%
\else%
\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}%
\fi%
\else%
\ifdim\mylength>0pt%
\rlap{\normalfont%
\kern\linewidth \kern\lst@numbersep%
\lst@numberstyle{\thelstnumber}}%
\else%
\llap{\normalfont%
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}%
\fi%
\fi%
}%
}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
\makeatother
\lstset{numbers=outside,frame=tb,numbersep=1em,language=pascal}
\begin{document}
\lipsum[1-2]
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\end{document}
输出