1

I take advantage of my previous question to ask this one, since from now on I want to implement my environments as much as possible and yet I have not found information about the topic of this question, and the objective of this question is not the same as the linked one.

I use the minted package to highlight my code in the documents, which uses the syntax: \begin{minted}[<options>]{<language used>}, and from the above linked question I got the code below. It is a new environment made by egreg, which draws the code background up to the longest code line, since by default minted draws the background up to \textwidth.

Code without modifications:

\documentclass{article}

\usepackage{minted}
\usemintedstyle[c++]{manni}

\usepackage{xcolor}
\definecolor{codebackground}{RGB}{240, 240, 235}

\begin{document} 
\begin{minted}[bgcolor=codebackground]{c} 
int main() {
  printf("hello, world");
  return 0;
}
\end{minted}
\end{document}

Code with modifications (the desired one):

\documentclass{article}

\usepackage{minted}
\usemintedstyle[c++]{manni}

\usepackage{xcolor}
\definecolor{codebackground}{RGB}{240, 240, 235}

\newsavebox{\mintedbox}
\newenvironment{boxminted}
{%
    \VerbatimEnvironment
    \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
    \begin{lrbox}{\mintedbox}
        \begin{minted}%
        }
        {%
        \end{minted}%
    \end{lrbox}%
    \noindent\colorbox{codebackground}{\usebox{\mintedbox}}%
}

\begin{document} 
\begin{boxminted}{c} 
int main() {
  printf("hello, world");
  return 0;
}
\end{boxminted}
\end{document}

The question is: how can the \newenvironment code be modified to use \begin{boxminted}[<options>]{<language used>}, and pass those <options> to the minted environment used inside the new environment? For example, pass as option [linenos = true] which set the code line number at the left of the line code.

6
  • You can already use \begin{boxminted}[<options>]{<language used>} as the \begin{minted} is the last thing inside of your begin-code, so it will be the last thing executed and will grab the arguments just like it should.
    – Skillmon
    Jun 13, 2019 at 21:45
  • For me it does not work. For example, if I say \begin{boxminted}[linenos = true]{c} the line numbers are not drawn at the left of the code lines, which I don't understand since it takes the argument {c} correctly.
    – Martín
    Jun 13, 2019 at 21:52
  • Most likely the linenos option isn't compatible with the boxing process.
    – Skillmon
    Jun 13, 2019 at 21:54
  • Checked. It's true. it seems it's the only option that does not work. I'll leave this question before deleting it to see if someone fix that in the linked question at the top since I already commented that bug in a comment in the accepted answer.
    – Martín
    Jun 13, 2019 at 22:02
  • 2
    Related/duplicate: tex.stackexchange.com/q/463954/4427
    – egreg
    Jun 13, 2019 at 22:38

1 Answer 1

3

From the manual of fancyvrb (which is used by the minted package):

4.2.2 BVerbatim environment

This environment puts the verbatim material in a TeX box. Some parameters do not work inside this environment (notably the framing ones), but two new ones are available: [...]

So the incompatibility of linenos arises from the use of \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}, which unfortunately is necessary to get the correct width of the contents. So line numbers won't be available with this. (One might be able to write a dirty hack, though, measuring the height of the box and putting the line numbers to the left by oneself).

EDIT: The following introduces a hacky way to get line numbers. Not sure how well it'll work, the small tests I've tried so far seem to work. Use boxminted* instead of boxminted for line numbers:

\documentclass{article}

\usepackage{expl3}

\usepackage{minted}
\usemintedstyle[c++]{manni}

\usepackage{xcolor}
\definecolor{codebackground}{RGB}{240, 240, 235}

\newsavebox{\mintedbox}
\newenvironment{boxminted}
{%
    \VerbatimEnvironment
    \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
    \begin{lrbox}{\mintedbox}
        \begin{minted}%
        }
        {%
        \end{minted}%
    \end{lrbox}%
    \noindent\colorbox{codebackground}{\usebox{\mintedbox}}%
    \typeout{Guessed lines:}
}
\ExplSyntaxOn
\int_new:N \l__martin_lino_int
\newenvironment{boxminted*}
  {%
    \VerbatimEnvironment
    \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
    \begin{lrbox}{\mintedbox}
      \begin{minted}%
  }
  {%
      \end{minted}%
    \end{lrbox}%
    \noindent
    \int_set:Nn \l__martin_lino_int
      { \fp_to_int:n { \ht\mintedbox/\baselineskip } }
    \llap
      {
        \begin{tabular}[b]{@{}r@{}}
          \int_step_function:nN { \l__martin_lino_int } \__martin_line:n
        \end{tabular}\hskip1.1em
      }
    \noindent\colorbox{codebackground}{\usebox{\mintedbox}}%
  }
\cs_new:Npn \__martin_line:n #1 { \tiny #1 \\ }
\ExplSyntaxOff

\begin{document} 
\begin{boxminted*}[linenos=true]{c} 
int main() {
  printf("hello, world");
  return 0;
  printf("hello, world");
  printf("hello, world");
  return 0;
  return 0;
}
\end{boxminted*}
\begin{minted}[linenos=true]{c} 
int main() {
  printf("hello, world");
  return 0;
  printf("hello, world");
  printf("hello, world");
  return 0;
  return 0;
  printf("hello, world");
  return 0;
  printf("hello, world");
  printf("hello, world");
  return 0;
  return 0;
}
\end{minted}
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .