我列出了 ruby 的设置(rubyl/rubyr),以防行号在左侧或右侧。
\lstdefinelanguage{rubyr}{
escapechar=\,
basicstyle=\scriptsize\ttfamily,
numbers=right, % <----
numberstyle=\scriptsize\ttfamily,
stepnumber=1,
numbersep=3pt,
showstringspaces=false,
breaklines=true,
frame=lines,
%backgroundcolor=\color{background},
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}
\lstdefinelanguage{rubyl}{
...
numbers=left,
...
}
重复一堆代码行似乎不太合适,那么有没有通过传递参数(数字=左或右)来设置的方法?
这是 lstlisting 环境的用法。
\begin{figure}[htbp]
\lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf}
\begin{lstlisting}[language=rubyr,firstnumber=1]
...
\end{lstlisting}
答案1
假设我理解您的问题,您只需将语句移动numbers=
到环境中的选项即可lstlisting
:
\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{ruby}{
escapechar=\,
basicstyle=\scriptsize\ttfamily,
numberstyle=\scriptsize\ttfamily,
stepnumber=1,
numbersep=3pt,
showstringspaces=false,
breaklines=true,
frame=lines,
%backgroundcolor=\color{background},
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}
\begin{document}
\lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf}
\begin{lstlisting}[language=ruby, numbers=right,firstnumber=1]
...
\end{lstlisting}
\begin{lstlisting}[language=ruby, numbers=left,firstnumber=1]
...
\end{lstlisting}
\end{document}
结果:
选择:
您可以将调用\lstdefinelanguage
包装在包装器中并传递numbers
参数:
\documentclass{article}
\usepackage{listings}
\newcommand{\defruby}[2]{%
\lstdefinelanguage{#1}{
escapechar=\,
basicstyle=\scriptsize\ttfamily,
numbers=#2, % <----
numberstyle=\scriptsize\ttfamily,
stepnumber=1,
numbersep=3pt,
showstringspaces=false,
breaklines=true,
frame=lines,
%backgroundcolor=\color{background},
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}}
\defruby{rubyr}{right}
\defruby{rubyl}{left}
\begin{document}
\lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf}
\begin{lstlisting}[language=rubyr,firstnumber=1]
...
\end{lstlisting}
\begin{lstlisting}[language=rubyl,firstnumber=1]
...
\end{lstlisting}
\end{document}
结果:与上相同。