2

I'd like to have the line numbers corresponding to code highlighted using the minted package displayed in the output of a preview environment. Perhaps because the line numbers are set in the margin, they turn out not to be displayed. I've used the xleftmargin option provided by the minted package, but it's been to no avail. How can this be done?

The document is compiled over pdfTeX.

Follows an MWE (the C++ code therein is nonsense, just for the sake of providing some content):

\documentclass{memoir}

\usepackage[active,pdftex,tightpage,psfixbb]{preview}
\usepackage{xcolor}
\usepackage{minted}

\pagestyle{empty}

\begin{document}
\begin{preview}
\begin{minted}[xleftmargin=\parindent,linenos]{cpp}
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

unsigned short i;

int main() {
  for (i = 0; i <= 5; i++) {
    // whatever
  }  
  return 0;  
}
\end{minted}
\end{preview}
\end{document}
4

The problem is that \parindent is zero when you evaluate it.

\documentclass{memoir}

\usepackage[active,pdftex,tightpage,psfixbb]{preview}
\usepackage{xcolor}
\usepackage{minted}

\pagestyle{empty}

\begin{document}
\begin{preview}
\begin{minted}[xleftmargin=20pt,linenos]{cpp}
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

unsigned short i;

int main() {
  for (i = 0; i <= 5; i++) {
    // whatever
  }  
  return 0;  
}
\end{minted}
\end{preview}
\end{document}

enter image description here

You might want to look at the standalone class:

\documentclass[
  class=memoir,
  varwidth,
  border={20 0 2 2},
]{standalone}

\usepackage{xcolor}
\usepackage{minted}

\begin{document}
\begin{minted}[linenos]{cpp}
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

unsigned short i;

int main() {
  for (i = 0; i <= 5; i++) {
    // whatever
  }  
  return 0;  
}
\end{minted}

\end{document}

enter image description here

1

In the meantime, I have myself found a simple workaround which makes sense in the overall context where my post came from. It is not in the strict sense an answer to my post because it involves wrapping the minted environment within a tikzpicture environment, but it works for me. I'll leave my "answer" unchecked for some time to see if somebody comes up with another not involving such a wrapping.

You can find below the pertinent lines to be added within my original MWE (I have also adjusted the value of the xleftmargin key).

\begin{preview}
\begin{tikzpicture}
  \node {
  \begin{minipage}{\textwidth}
  \begin{minted}[xleftmargin=1em,linenos]{cpp}
    // Whatever C++ code to be displayed 
  \end{minted}
  \end{minipage}      
  };
\end{tikzpicture}
\end{preview}
  • I can see only part of the numbers to the left. – giordano Jan 14 '16 at 11:12
  • It should be a matter of readjusting the xleftmargin value. – Marcos Jan 14 '16 at 11:25

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.