Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to reference other parts of my document (e.g. equations, figures) inside of some source code. Is there an escape sequence such that \ref will be parsed instead of printed as a literal \ref?

My minimal example is as follows:

\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}

\begin{document}
This is the pythagorean theorem:
\begin{equation}\label{eq:pyth}
a^2+b^2=c^2
\end{equation}

I can reference it normally: Equation \ref{eq:pyth}.

I can write a code example that won't work:
\begin{verbatim}
# An R function to solve for c.
# See Equation \ref{eq:pyth} for details.
solve.for.c <- function(a,b){
  return( sqrt(a^2 + b^2))
}
\end{verbatim}

And one that works, but is very ugly and hard to maintain: \\
\verb@# An R function to solve for c.@ \\
\verb@# See Equation@ \ref{eq:pyth} \verb@for details.@ \\
\verb@solve.for.c <- function(a,b){@ \\
\verb@solve  return( sqrt(a^2 + b^2))@ \\
\verb@}@ \\

How can I do this better?
\end{document}

Any thoughts on how to do this better? Is there a simple feature I'm missing?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

You're not missing simple features; the standard verbatim environment doesn't allow for interpreting commands. However, the fancyvrb package has this facility:

\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}
\usepackage{fancyvrb}

\begin{document}
This is the pythagorean theorem:
\begin{equation}\label{eq:pyth}
a^2+b^2=c^2
\end{equation}

I can reference it normally: Equation \ref{eq:pyth}.

I can write a code example that will work:
\begin{Verbatim}[commandchars=\\\[\]]
# An R function to solve for c.
# See Equation \ref[eq:pyth] for details.
solve.for.c <- function(a,b){
  return( sqrt(a^2 + b^2))
}
\end{Verbatim}

And also a \texttt{lstlisting} environment:
\begin{lstlisting}[language=R,escapechar=',columns=fullflexible]
# An R function to solve for c.
# See Equation '\ref{eq:pyth}' for details.
solve.for.c <- function(a,b){
  return( sqrt(a^2 + b^2))
}
\end{lstlisting}

\end{document}

For commandchars in Verbatim you have to specify three characters (escaped with the backslash) that aren't otherwise used in the environment's text. The same for escapechar with lstlisting. These characters may be chosen "locally".

Take your pick.

enter image description here

share|improve this answer
    
Precisely what I was looking for. Thanks! –  Nathan VanHoudnos Jul 4 '12 at 21:40

enter image description here

If you have three handy characters that don't appear in the verbatim text you can use them instead of \ { } for example

\documentclass{article}

\usepackage{amsmath}

\makeatletter
\let\oldv\@verbatim
\def\@verbatim{\oldv\catcode`\!=0 \catcode`\`=1 \catcode`\'=2 }
\makeatother

\begin{document}
This is the pythagorean theorem:
\begin{equation}\label{eq:pyth}
a^2+b^2=c^2
\end{equation}

I can reference it normally: Equation \ref{eq:pyth}.

I can write a code example that won't work:
\begin{verbatim}
# An R function to solve for c.
# See Equation !ref`eq:pyth' for details.
solve.for.c <- function(a,b){
  return( sqrt(a^2 + b^2))
}
\end{verbatim}



How can I do this better?
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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