4

How I can center a line code and numbering it like in the picture? Is there a command that works inside a lstlisting environment? Really thanks in advance

enter image description here

  • And what does this number represent? Is it a already existing counter? Is a dedicated counter for listings? Do you need this to have the possibility of cross-referencing? – Gonzalo Medina Jul 11 '15 at 19:02
  • Yes it is already existing and I would like to use it for cross-referencing. In the (piece of) document that I posted it is a counter that counts even for equation. For example: Equation1 (1) <br/> Equation 2 (2) <br/> LineOfCode (3) <br/> Equation 3 (4) <br/> and so on – Diego Ceglia Jul 11 '15 at 19:10
  • is it a single line or part of a longer sequence? – user2478 Jul 11 '15 at 19:46
2

Here I use my numberedblock package, that allows labeled code blocks that can be referenced. I use brackets instead of parens, so as not to confuse with equation numbering. With its built-in macros and environments, one can only do code blocks of fixed indent. However, by employing some additional code, centering can be achieved.

\documentclass{article}
\usepackage{numberedblock,lipsum,stackengine}
\setstackEOL{\\}
\setstackgap{L}{\normalbaselineskip}
              \setlength\maxblocklabelsize{0in}
                     \setlength\blockindent{.2in}
 \renewcommand\blocklabel[1]{\llap{[\textit{\arabic{#1}}]}}
\begin{document}
\lipsum[1]
as enthalpy\ldots from the old pressure

\begin{numVblock}[\nbVlabel{cd:a}]
For fixed indent code
Can be verbatim &^%\\
solve(UEqn == - fvc::grad(p));
\end{numVblock}
\setlength\blockindent{0in}

\begin{verbbox}
For centered code
Can be verbatim &^%\\
solve(UEqn == - fvc::grad(p));
\end{verbbox}
\nbVlabel{cd:b}\numblock{\makebox[\textwidth]{\theverbbox}}

In code blocks~\ref{cd:a} and \ref{cd:b}, \lipsum[2]
\end{document}

enter image description here

2

You want it centered and using the counter for equations, so the easiest thing would be to use an equation and write it inside \hbox with the desired format:

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}

\lstset{basicstyle=\ttfamily}

\begin{document}

some test text and some more text and some more text and some more text and some more text and some more text and some more text and some more text
\begin{equation}
\hbox{\lstinline!solve(UEqn == - fvc::grad(p));!}
\end{equation}
some test text and some more text and some more text and some more text and some more text and some more text and some more text and some more text

\end{document}

enter image description here

  • This seems the best option for me, thank you very much Gonzalo – Diego Ceglia Jul 11 '15 at 20:59
  • 1
    @DiegoCeglia You're welcome! I updated the answer since just now I read you were using the listings package. I show how my answer works using \lstinline. – Gonzalo Medina Jul 11 '15 at 21:04
  • 1
    This is one of the few cases where I'd recommend \hbox and not \text, if you want to use the full force of \lstinline (with category code changes). – egreg Jul 11 '15 at 21:11
  • @egreg You are right. I changed accordingly. Thanks. – Gonzalo Medina Jul 11 '15 at 21:58
1

Use the escapeinside option:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,escapeinside=`'}
\newcounter{cnt}
\newcommand\Label[1]{\hfill\refstepcounter{cnt}(\thecnt)\label{#1}}
\begin{document}

\begin{lstlisting}
solve(UEqn == - fvc::grad(p)); `\Label{foo}'
\end{lstlisting}

\begin{lstlisting}
solve(UEqn == - fvc::grad(p)); `\Label{bar}'
\end{lstlisting}


also seen in \ref{foo} and \ref{bar} \ldots
\end{document}

enter image description here

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.