10

I'm using the minted package for code highlighting. The below is just a minimal example. What I find annoying about this package is that line numbers appear in the margin.

enter image description here

This causes several problems with \items but also in twocolumn articles. What I would like is to have the line numbers aligned with the normal text, like so:

enter image description here

I have read through the documentation, but there doesn't seem to be such an option. The numbers=right option doesn't help me either: I don't want numbers on the right, and even if I would, that option places numbers in the right margin.

I have tried putting it altogether in a minipage (i.e. \begin{minipage}{\linewidth}\begin{minted} ... \end{minted}\end{minipage}), but couldn't get spacing work properly.

What would be the preferred way to move the numbers to the right?

\documentclass[10pt]{article}

\usepackage{minted,lipsum}

\begin{document}

\lipsum[1]

\begin{minted}[linenos,breaklines]{text}
Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Mauris ut leo.
Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices.
\end{minted}

\lipsum[2]

\end{document}
10

Use xleftmargin:

\documentclass[10pt]{article}

\usepackage{minted,lipsum}

\begin{document}

\lipsum[1]

\begin{minted}[xleftmargin=\parindent,linenos,breaklines]{text}
Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Mauris ut leo.
Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices.
\end{minted}

\lipsum[2]

\end{document}

enter image description here

You may also want to modify numbersep; the default is numbersep=12pt.

For instance, if you want that the line numbers (two digits) are flush with the left margin, you can do

\newlength{\mintednumbersep}
\AtBeginDocument{%
  \sbox0{\tiny00}%
  \setlength\mintednumbersep{\parindent}%
  \addtolength\mintednumbersep{-\wd0}%
}

and then set numbersep=\mintednumbersep.

| improve this answer | |
  • I searched for number in the documentation, not for margin... Thanks! – Keelan Jun 25 '15 at 20:39

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.