1

I have a lstlisting environment that shows a line number either left or right side of the document.

Setting up lstlisting environment by passing parameters

Setting the ruby code in a two-column document, I keep changing the numers parameter depending on the final layout of the document. The following screen capture shows the wrong number location as a final layout.

enter image description here

\end{lstlisting}
\begin{lstlisting}[language=ruby, numbers=left,firstnumber=1]
...
\end{lstlisting}

Is there an way to setup the lstlisting environment based on which side of the document the lstlisting environment is located?

ADDED

I use IEEEtran class, which has its own options. One can find it from http://tug.ctan.org/tex-archive/macros/latex/contrib/IEEEtran/IEEEtran.cls

\documentclass[conference]{IEEEtran}
\usepackage{cite}

...

\DeclareOption{oneside}{\@twosidefalse\@mparswitchfalse
                        \CLASSOPTIONonesidetrue\CLASSOPTIONtwosidefalse}
\DeclareOption{twoside}{\@twosidetrue\@mparswitchtrue
                        \CLASSOPTIONtwosidetrue\CLASSOPTIONonesidefalse}

\DeclareOption{onecolumn}{\CLASSOPTIONonecolumntrue\CLASSOPTIONtwocolumnfalse}
\DeclareOption{twocolumn}{\CLASSOPTIONtwocolumntrue\CLASSOPTIONonecolumnfalse}

ADDED 2

This code works fine.

\documentclass[conference]{IEEEtran}
\usepackage{listings}
\usepackage{lipsum}

\lstdefinelanguage{rruby}{
        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},
            }

\lstset{
    basicstyle=\ttfamily\scriptsize,
}
\makeatletter
\lstnewenvironment{ruby}{
        \if@firstcolumn       
            \lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf, language=rruby, numbers=left}
        \else
            \lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf, language=rruby, numbers=right}
        \fi
}{}
\makeatother


\begin{document}
\lipsum

\begin{ruby}
...
\end{ruby}
\begin{lstlisting}[language=ruby, numbers=left,firstnumber=1]
...
\end{lstlisting}
\end{document}

ADDED 3

When the listings environment is in the float environment, the code does not work.

Setting different environment setup depending on the location in two column (also considering the figure environment)

2
  • There are ways, but it will depend on your approach to two columns. Would you mind putting together a MWE?
    – Guho
    Jan 8, 2016 at 4:39
  • @Guho: I updated my post.
    – prosseek
    Jan 8, 2016 at 4:54

1 Answer 1

2

The command \if@firstcolumn can detect which column you are in; then, you can use \lstnewenvironment to create an environment that sets the numbering before each listing. (There may be other solutions, such as a hook at the top of each column, but I don't know for certain.)

\makeatletter
\lstnewenvironment{tclist}{
    \if@firstcolumn
      \lstset{numbers=left}
    \else
      \lstset{numbers=right}
    \fi
}{}
\makeatother

Then, you can use \begin{tclist}...\end{tclist} exactly as you would use \begin{lstlisting}...\end{lstlisting} in your document.

Here's an example document:

\documentclass[twocolumn]{IEEEtran}
\usepackage{listings}
\lstset{
    basicstyle=\ttfamily\scriptsize,
}
\makeatletter
\lstnewenvironment{tclist}{
% ...same definition as above goes here...
\makeatother
\usepackage{lipsum}
\begin{document}
\lipsum[1-2]
\begin{tclist}
#!/bin/bash

echo hello, world.
\end{tclist}
\lipsum[3-5]
\begin{tclist}
#!/bin/env python3

def main():
    print("Hello, world.")

if __name__ == '__main__':
    main()
\end{tclist}
\end{document}

And here's the output: enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .