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.
\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.\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.linenos
option isn't compatible with the boxing process.