1

I asked about using listings environment to put the line numbers left or right side according to the location.

Setting different environment setup depending on the location in two column

The code works fine, but when I put the environment in the figure environment, the direction of the line numbers got it wrong.

enter image description here

Is there a possible to locate the correct side of the document considering the case when located in float environment? I need the float environment for making captions for the source code.

This is the code:

% https://tex.stackexchange.com/questions/286592/setting-different-environment-setup-depending-on-the-location-in-two-column
\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{figure}[htbp]
\begin{ruby}
...
\end{ruby}
\end{figure}

\lipsum

\begin{figure}[htbp]
\begin{ruby}
...
\end{ruby}
\end{figure}

\end{document}
prosseek
  • 6,033

1 Answers1

1

For making a caption, I could use caption package. Hints from Label and caption without float.

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

\DeclareCaptionType{mytype}[Code][List of mytype]
\newenvironment{code}{}{}
\definecolor{delim}{RGB}{20,105,176}

\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{code}
\begin{ruby}
...
\end{ruby}
\captionof{mytype}{Something in my type}
\end{code}

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

enter image description here

prosseek
  • 6,033