4

I am currently trying to reference a vebatim block, but aren't sure why this is not work...

\begin{verbatim}
ref: 
hyp:    
\label{verb:deletion}
\end{verbatim}

Edit

I guess it not possible to do this with verbatim.. I wanted to highlight a feature in the verbatim block, but can this be done in a different way such that it also can be referenced.

It would be appreciated if the solution would not involve messing with current commands, or creating new ones. Isn't there a already a package capable of doing this?

  • verbatim='write literally', i.e. the commands are meaningless text to LaTeX, not commands to be expanded – user31729 Dec 27 '16 at 21:08
  • What should \ref{verb:deletion} print and/or how is the verbatim environment numbered? – Werner Dec 27 '16 at 21:08
  • 1
    Since verbatim blocks are not numbered, what exactly are you hoping to remember with the label? – Steven B. Segletes Dec 27 '16 at 21:08
  • I guess i am not able to use verbatim.. that output something in a similar format, but where it is possible to put a label on it.. – dfh Dec 27 '16 at 21:14
  • @dfh: Are you looking for something like displaying programming code? Then listings or minted are the alternatives, in my point of view, or tcolorbox and its verbatim box features! – user31729 Dec 27 '16 at 21:17
4
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.88]{beramono}    
\usepackage{listings}
\lstset{basicstyle=\small\ttfamily}

\begin{document}
\begin{lstlisting}[label=verb1,caption=First verbatim,float,frame=tb]
Foo stuff to be displayed verbatim

Something like \LaTeXe
\end{lstlisting}

In~\ref{verb1} we see some verbatim code
\end{document}

enter image description here

  • I like this much better.. I is possible to center in the text in the listing? – dfh Dec 27 '16 at 21:38
  • no, not in an easy way. You can get the caption on the left. That is easy. – user2478 Dec 27 '16 at 21:56
5

Since the verbatim environment does not have a counter itself (at least in the verbatim package version), a \label does not have the intended effect, apart from the fact that \label appearing inside of the verbatim environment is totally ignored as a command sequence and being treated as pure text, having no further meaning.

However, it is possible to 'trick' the verbatim environment using a counter and having some kind of a lead-in line, displaying the number, in this case, a reference to the 'correct' number is possible.

\documentclass{article}

\usepackage{etoolbox}

\newcounter{myverb}
\usepackage{verbatim}

\BeforeBeginEnvironment{verbatim}{%
\refstepcounter{myverb}%
\noindent\textbf{Verbatim stuff \themyverb}%
}

\begin{document}
\begin{verbatim}
Foo stuff to be displayed verbatim

Something like \LaTeXe
\end{verbatim}
\label{Foo}

In \ref{Foo} we see some foo stuff, but not \LaTeXe\ as expected!
\end{document}

enter image description here

  • Is it possible to place the Verbatim stuff text somewhere? – dfh Dec 27 '16 at 21:28
  • Possibly like in equation? – dfh Dec 27 '16 at 21:29
  • @dfh: Well, yes, as long it is either before or after the environment – user31729 Dec 27 '16 at 21:29
  • How do i move the text? – dfh Dec 27 '16 at 21:30
  • so the number is to the side and not at the top.. – dfh Dec 27 '16 at 21:30

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.