3

I am trying to use the lstlisting environment to insert R code into my LaTeX document. What I cannot figure out is how to get LaTeX to number these equations so that I can refer back to them later in the text. I want to do something like this:

\begin{lstlisting}[language=R]
lm.model <- lm(Total.Score ~ Game.Num, data = sid1133)
\end{lstlisting}\label{eq:lm.model}

To be clear -- I know I can do this:

\begin{lstlisting}[language=R, caption={Standard Linear Regression Model Equation}, label=foobar]
lm.model <- lm(Total.Score ~ Game.Num, data = sid1133)
\end{lstlisting}

However, I want to put the equation number to the right of the equation -- not in a separate header at top, and I want to be able to refer to it later in the text.

3

To be able to create an R-listing (or actually any listing) inside \begin{equation} ... \end{equation}, you'll need to use \lstinline instead of \begin{lstlisting} ... \end{lstlisting}. To start and end the code snippet, you can use any character which is not used within the code. A popular choice is e.g. | or $:

\lstinline[language=R]|lm.model <- lm(Total.Score ~ Game.Num, data = sid1133)|

but again, it doesn't matter as long as that character does not appear in your code snippet.

To be able to place this \lstinline within an equation, you have to "escape" math mode first, so you are in the "normal" text mode. This is done with \text{any content which is not math}. So together, this is

\documentclass{article}
\usepackage{listings}
\usepackage{amsmath}
\begin{document}
As described in \eqref{eq:lm.eq:lm.model}, this is very important.
\begin{equation} \label{eq:lm.eq:lm.model}
    \text{\lstinline[language=R]|lm.model <- lm(Total.Score ~ Game.Num, data = sid1133)|}
\end{equation}
\end{document}

enter image description here

2

Typically references to listings are done either to the listing as a whole (and therefore would have a caption) or to the line numbers within the listing.

Below I follow the second approach as it seems strange to provide a single-line listing with a caption. Even though the numbering is set on the left, you can also place it on the right. See section 5.6 Line numbers of the listings documentation for more options regarding the formatting of line numbers inside a listing.

enter image description here

\documentclass{article}

\usepackage[margin=1in]{geometry}% Just for this example

\usepackage{listings}
\lstnewenvironment{rcode}[1][]
  {\lstset{
     language=R,
     numbers=left,
     numberstyle=\footnotesize,
     escapeinside=@@,
     #1}}
  {}
\newsavebox{\codebox}

\begin{document}

\begin{rcode}
lm.model <- lm(Total.Score ~ Game.Num, data = sid1133) @\label{r:lm.model-1}@
\end{rcode}

See Line~\ref{r:lm.model-1} or Line~\ref{r:lm.model-2}.

\begin{rcode}[firstnumber=200]
lm.model <- lm(Total.Score ~ Game.Num, data = sid1133) @\label{r:lm.model-2}@
\end{rcode}

\begin{lrbox}{\codebox}
\begin{lstlisting}[language=R]
lm.model <- lm(Total.Score ~ Game.Num, data = sid1133)
\end{lstlisting}
\end{lrbox}

Alternatively, see~(\ref{eq:lm.model-1}):
\begin{equation}
  \usebox{\codebox} \label{eq:lm.model-1}
\end{equation}

\begin{lrbox}{\codebox}
\lstinline[language=R]|lm.model <- lm(Total.Score ~ Game.Num, data = sid1133)|
\end{lrbox}

Alternatively, see~(\ref{eq:lm.model-2}):
\begin{equation}
  \usebox{\codebox} \label{eq:lm.model-2}
\end{equation}

\end{document}

In order to properly reference the line numbers, you'll have to escape to insert an appropriate \label.

If you really wish to include the listing code as an equation, you may want to consider storing the listing in a box before setting it (above I use \begin{lrbox}{\codebox} ... \end{lrbox}). Note the difference between the way content is set under the lstlisting environment and \lstinline macro.

0

This example is best \begin{equation} \hbox{ \begin{lstlisting} @@ (?: (?[a-zA-Z]+) (?:@(?[,0-9\x20]+))? )? @@ (?.+?) @END@ (?.?\n|.) \end{lstlisting}} \end{equation}

  • 3
    Welcome to TeX.SE. Please add a few sentences to explain what your code does and how it addresses the OP's question. – Mico Aug 4 '17 at 19:12

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.