如何使用以零开头的行号?

如何使用以零开头的行号?

我使用该listings软件包来获取源代码,并阅读了所有文档。但是,行号前导零似乎尚未作为标准选项实现。我如何重新定义行号以使其使用额外的零?

Given: 1 ... 10 ... 100 ... 1000 ....
Want: 01 ... 10 ... 99 (if largest printed number <100)
Want: 001 ... 010 ... 100 ... 999 (if largest printed number <1000) etc.

或者:如何重新定义位数最多的数字后的对齐?

Given:    Want:
1            1
...        ...
10          10
...        ...
100        100

添加乳胶样品:

\documentclass{article}
\usepackage{listings} 
\lstset{language=Ruby,
        basicstyle=\ttfamily, 
        numbers=right,
        numberstyle=\ttfamily, 
      % solution for right alignment:
      % numberstyle=\ttfamily\makebox[3em][r],
        showstringspaces=false}

\begin{document}
\begin{lstlisting}
#!/usr/bin/env ruby
# test of latex listings package
# uses line numbering with 1-12. 
# should be using leading zeros, like 01-12.
puts "line number 05."
puts "line number 06."
puts "line number 07."    
puts "line number 08."
puts "line number 09."
puts "line number 10."
puts "line number 11."
puts "line number 12."
\end{lstlisting}
\begin{lstlisting}[firstnumber=98]
puts "line number 098."
puts "line number 099."
puts "line number 100."
puts "line number 101."
puts "line number 102."
\end{lstlisting}
\begin{lstlisting}[firstnumber=998]
puts "line number 0998."
puts "line number 0999."
puts "line number 1000."
puts "line number 1001."
puts "line number 1002."
\end{lstlisting}
\begin{lstlisting}[firstnumber=9998]
puts "line number 09998."
puts "line number 09999."
puts "line number 10000."
puts "line number 10001."
puts "line number 10002."
\end{lstlisting}
\begin{lstlisting}[firstnumber=8]
puts "line number 08."
puts "line number 09."
puts "line number 10."
puts "line number 11."
puts "line number 12."
\end{lstlisting}
\end{document}

答案1

这是一种尝试自动确定列表中行数的方法:

  • 为每个列表分配一个唯一的(增量)编号
  • 使用机制保存每个列表末尾的计数器值lstnumber(但太大)\label
  • lstnumber使用机制检索计数器的保存值\ref(因此需要两次运行才正确)
  • 减少恢复的值,并尝试计算其中的数字个数
  • \padzeroes相应设置
  • 设置从包中numberstyle调用的,使用上一步中设置的数字\decimalnumfmtcount

这可能不是最优雅的方式......

代码:

\documentclass{article}
\usepackage{listings}
\lstset{language=Ruby,
        basicstyle=\ttfamily,
        numbers=right,
        numberstyle=\ttfamily\boxeddecimalnum,
        showstringspaces=false}
\errorcontextlines=\maxdimen

\newcommand*{\boxeddecimalnum}[1]{\makebox[3em][r]{\decimalnum{#1}}}
\usepackage{refcount}
\usepackage{fmtcount}
\usepackage{intcalc}

\def\getnumdigitsaux #1{%
    \ifx#1\quark
    \expandafter\relax
    \else
    +1\expandafter\getnumdigitsaux
    \fi
}

\def\quark{\quark}
\newcommand\getnumdigits[1]{%
    \the\numexpr\getnumdigitsaux #1\quark
}

\newcounter{lstuniquenumber}
\makeatletter
\lst@AddToHook{Init}{\stepcounter{lstuniquenumber}\edef\lastlineincurrentlisting{\intcalcDec{\getrefnumber{lastlineinlisting\thelstuniquenumber}}}\padzeroes[\expandafter\getnumdigits\expandafter{\lastlineincurrentlisting}]}% at the start of each listing, count the listing, and try to restore the line number saved in the previous run
\lst@AddToHook{DeInit}{\label{lastlineinlisting\thelstuniquenumber}}% at the end of each listing, save one past the last line number
\makeatother

\begin{document}

\begin{lstlisting}
#!/usr/bin/env ruby
# test of latex listings package
# uses line numbering with 1-12. 
# should be using leading zeros, like 01-12.
puts "line number 05."
puts "line number 06."
puts "line number 07."
puts "line number 08."
puts "line number 09."
puts "line number 10."
puts "line number 11."
puts "line number 12."
\end{lstlisting}
\begin{lstlisting}[firstnumber=98]
puts "line number 098."
puts "line number 099."
puts "line number 100."
puts "line number 101."
puts "line number 102."
\end{lstlisting}
\begin{lstlisting}[firstnumber=998]
puts "line number 0998."
puts "line number 0999."
puts "line number 1000."
puts "line number 1001."
puts "line number 1002."
\end{lstlisting}
\begin{lstlisting}[firstnumber=9998]
puts "line number 09998."
puts "line number 09999."
puts "line number 10000."
puts "line number 10001."
puts "line number 10002."
\end{lstlisting}

\end{document}

输出:

输出说明

答案2

要为 10 以下的数字添加一个前导零,只需\ifnum\value{lstnumber}<10 0\fi在数字前面添加即可。获取 的最大行数要困难得多。对于长度超过 99 行的数字,lstlisting重新定义适当的值会更容易。它们也添加到前面。\thelstnumber\ifnum\value{lstnumber}<100 0\fi

\documentclass{article}
\usepackage{listings} 
\lstset{language=Ruby,
        basicstyle=\ttfamily,
        numbers=right,
        numberstyle=\ttfamily,
        showstringspaces=false}

% solution for right alignment provided by Peter works!
\renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}

% solution for leading zeros is still pending!

\begin{document}
\begin{lstlisting}
#!/usr/bin/env ruby
# test of latex listings package
# uses line numbering with 1-12. 
# should be using leading zeros, like 01-12.
puts "line number 05."
puts "line number 06."
puts "line number 07."
puts "line number 08."
puts "line number 09."
puts "line number 10."
puts "line number 11."
puts "line number 12."
\end{lstlisting}
\end{document}

相关内容