2

I began using LaTeX just yesterday and I'm stuck on a frustrating problem. I need to automatically include a lot of source code into my document and getting it code-highlighted, row-numbered, labeled and indexed. I've done most of the job, except few points. First of all, I'm using different languages: PHP, JS, PostgreSQL, R and SAS. I've found that listings doesn't correctly treat /**/ SAS comments, so I decided to use the SASnRdisplay package which seems to be a sort of "extension" of the listings package. They share a lot of aspects. After a lot of tricking I finally got satisfied with the results.

What I need to do now is creating a list of listings containing the references to the listings generated by listings and the listings generated by SASnRdisplay using a single common numeration. At the moment I have basically two numberings: the listings numbering and the SASnRdisplay numbering.

I've tried using \setcounter{lstlisting}{3} but it doesn't seem to work.

Here's an example to test the \lstlistoflistings command (the other features are not implemented here for simplicity):

\documentclass[a4paper,twoside]{memoir}


\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{SASnRdisplay}


\begin{document}

\begin{lstlisting}[language=PHP, caption=First listing - using package ``listings'']
    $var = "value";
\end{lstlisting}


\begin{lstlisting}[language=Java, caption=Second listing - using package ``listings'']
    console.log('0123456789');
\end{lstlisting}



\begin{Rcode}[caption=Third listing - using package ``SASnRdisplay'']
    df <- c(1,2,3)
\end{Rcode}


\begin{SAScode}[caption=Fourth listing - using package ``SASnRdisplay'']
    /* comment */
    PROC PRINT data=mydata; VAR v1; RUN;
\end{SAScode}



\lstlistoflistings

\end{document}

And here's the output: output

  • Probably this can be solved by knowing the counter name used by SASnRdisplay. The other solution should be something like "unifying" the two counters, which is more complex than just incrementing/updating one of them. So my question goes in this direction.. but probably there's something else that I'm ignoring. – pietrop May 29 '14 at 18:10
2

I had a look at the source code of the SASnRdisplay package. It defines a macro called \SNRletcountercounter that overrides the normal numbering scheme of listings carried out by the listings package:

% This is inspired from the memoir class, it enables us to alias the
% listings counter such that we can have different series
% It will locally make one counter behave like another counter
% it also handles the \the<counter>
\newcommand*{\SnRletcountercounter}[2]{%
   \csletcs{c@#1}{c@#2}
   \csletcs{the#1}{the#2}
   \SnRHrefNumber{#2}
}

Redefining \SNRletcountercounter to \@gobbletwo cancels the effects of that macro and yields the desired output.

enter image description here

\documentclass[a4paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{SASnRdisplay}
\makeatletter
\let\SnRletcountercounter\@gobbletwo
\makeatother


\begin{document}

\begin{lstlisting}[language=PHP, caption=First listing - using package ``listings'']
    $var = "value";
\end{lstlisting}

\begin{lstlisting}[language=Java, caption=Second listing - using package ``listings'']
    console.log('0123456789');
\end{lstlisting}

\begin{Rcode}[caption=Third listing - using package ``SASnRdisplay'']
    df <- c(1,2,3)
\end{Rcode}

\begin{SAScode}[caption=Fourth listing - using package ``SASnRdisplay'']
    /* comment */
    PROC PRINT data=mydata; VAR v1; RUN;
\end{SAScode}

\lstlistoflistings

\end{document}
|improve this answer|||||
  • Thank you, Jubobs! In the meanwhile I've found an alternative solution which works too. I also had a look to the original source code and I've found the name of the SASnRdisplay counter. You can set it to the current value of the lstlisting counter "on the fly". If the first SASnRdisplay code was R you need to call this: \setcounter{Rcode}{\value{lstlisting}} otherwise if it was a SAS code you need to call this: \setcounter{SAScode}{\value{lstlisting}} – pietrop May 29 '14 at 19:59
  • EDIT: your solution works but generates a problem. Before every caption of each SASnRdisplay listing in my document LaTeX generates a string stating "lstlistingRcode". I think I'll go with the counter solution because it doesn't generate any sort of problem.. – pietrop May 29 '14 at 23:14
  • @pietrop See my edit. – jub0bs May 30 '14 at 6:00
  • It generates the following error (in my document I define several commands): ! LaTeX Error: Command \@ already defined.Or name \end... illegal, see p.192 of the manual.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \newcommand{\@annoAccademico}{2013/2014} – pietrop May 31 '14 at 9:15
  • @pietrop I don't get any error. You've probably forgotten the surrounding \makeatletter and \makeatother; those are important. – jub0bs May 31 '14 at 16:51
0

Jubobs solution (\let\SnRletcountercounter\relax) works but a string stating "lstlistingRcode" may appear before every listing generated by SASnRdisplay. The other way to go is to update the listings counter using the last counter value used. If the last listing was from the listings package and you want to add a listing from the SASnRdisplay package, then add the following line between the two listings: \setcounter{Rcode}{\value{lstlisting}} otherwise add the following one: \setcounter{lstlisting}{\value{Rcode}}

If Rcode doesn't work try using SAScode.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.