0

I am new to Latex so I might be missing something basic. I need a custom command for code snippets. Normally I would write this:

\begin{minted}
[
frame=lines,
firstnumber=182,
framesep=2mm,
baselinestretch=1.2,
bgcolor=white,
fontsize=\footnotesize,
linenos
]{java}
int x = 3;
int y = x + 3;
\end{minted}

What I want to be able to use is this:

\code{182}{
int x = 3;
int y = x + 3;
}

This is my attempt at defining it:

\newcommand{\code}[2]{
\begin{minted}
[
frame=lines,
firstnumber=#1,
framesep=2mm,
baselinestretch=1.2,
bgcolor=white,
fontsize=\footnotesize,
linenos
]{java}
#2
\end{minted}
}

The \begin{minted} part and arguments are interpreted correctly, but my code (#2) is not printed, and the {minted} section ends when ended explicitly by a subsequent \end{minted}

chtorr97
  • 21
  • 4

1 Answers1

2

The comment from Tiuri really helped!

This is what I ended up doing in the preamble:

\usepackage{minted}
\usepackage{environ}

\newenvironment{code}[1]
 {\VerbatimEnvironment
  \begin{minted}[
    frame=lines,
    firstnumber=#1,
    framesep=2mm,
    baselinestretch=1.2,
    fontsize=\footnotesize,
    linenos]{java}}
 {\end{minted}}

and then:

\begin{code}{411}
int x = 3;
int y = x + 3;
\end{code}

This allows me to have blocks of code already formatted for which I only need to specify the starting line number and the code itself.

Output:

enter image description here

chtorr97
  • 21
  • 4