3

I have listing setups (rubyl/rubyr) for ruby for the case the line numbering is on the left or right hand side.

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

Duplicating a bunch of lines of code doesn't seem proper, so is there a way to setting up by passing parameters (numbers = left or right)?

This is the usage of the lstlisting environment.

\begin{figure}[htbp]
\lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf}
\begin{lstlisting}[language=rubyr,firstnumber=1]
...
\end{lstlisting}
prosseek
  • 6,033

1 Answers1

3

Assuming I understand your question, you can just move the numbers= statement to the options in the lstlisting environment:

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

Result:

ruby right-left result

Alternative:

You could wrap the \lstdefinelanguage call in a wrapper and pass the numbers argument:

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

Result: Same as above.

Guho
  • 6,115