5

I tried to create a new environment in which I wanted to use minted. Somehow it didn't really work and I found these two posts: 1. 2.

I then copied the code and tried to modify it for my needs. I want to be able to use \begin{code}{label}. This is what I have:

\newenvironment{code}[2]
{\VerbatimEnvironment
\minted@resetoptions
\setkeys{minted@opt}{frame=lines}% from fancyvrb
\renewcommand{\minted@proglang}[1]{#1}
\begin{figure}[htp]%% default placing
    \centering
    \label{#2}
    \begin{VerbatimOut}{\jobname.pyg}}
    {\end{VerbatimOut}
    \minted@pygmentize{\minted@proglang{}}
    \DeleteFile{\jobname.pyg}
\end{figure}}
\makeatother

I get several "FancyVerb" errors and undefined control sequences.

EDIT

Basically I want to define a new environment that produces the following:

\begin{minted}
    [
    frame=lines,
    framesep=2mm,
    linenos,
    label=TestLabel
    ]
    {java}
    while(true){
    //Do stuff
    }
    \end{minted}

by just using \begin{code}{TestLabel}...\end{code} enter image description here

  • 1
    You seem to be missing a makeatletter - check the source of your copy/paste. – Ethan Bolker Dec 10 '16 at 15:25
  • It would help if you would add the errors you got to the question. – Reinstate Monica - M. Schröder Dec 10 '16 at 15:42
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Reinstate Monica - M. Schröder Dec 10 '16 at 15:43
6

I think you're overcomplicating things:

\documentclass{article}
\usepackage{minted}
\usepackage{lipsum}

\newenvironment{code}[4][htp]
 {\VerbatimEnvironment
  \begin{figure}[#1]
  \centering
  \caption{#3}\label{#4}
  \begin{minted}[frame=lines]{#2}}
 {\end{minted}\end{figure}}

\begin{document}

\lipsum[1]

\begin{code}{latex}{A caption}{label1}
Test of \LaTeX code
\end{code}

\lipsum[2]

\begin{code}[bp]{latex}{A caption}{label2}
Test of \LaTeX code
\end{code}

\lipsum

\end{document}

enter image description here

Another version, where the argument refers to label=; I also changed figure into the more proper listing environment.

\documentclass{article}
\usepackage{minted}
\usepackage{lipsum}

\newenvironment{code}[2][htp]
 {\VerbatimEnvironment
  \begin{listing}[#1]
  \centering
  \begin{minted}[frame=lines,framesep=2mm,linenos,label=#2]{java}}
 {\end{minted}\end{listing}}

\begin{document}

\lipsum[1]

\begin{code}{Code 1}
while(true){
    //Do stuff
}
\end{code}

\lipsum[2]

\begin{code}[bp]{Code 2}
while(true){
    //Do stuff
}
\end{code}

\lipsum

\end{document}

enter image description here

  • Thank you @egreg but there are some things I wanted to change: I always want to use java ( I tried to change the #2 into java) And I also don't want to set a caption (only a label). Also I noticed that the linespan is too small and the label is not showing. how would I change that? – jobr97 Dec 12 '16 at 14:44
  • @jobr97 Setting a label without a caption is useless. – egreg Dec 12 '16 at 14:54
  • but if I define a label in the options it shows up on top of the code block... I added an example to my post – jobr97 Dec 12 '16 at 14:59
  • 1
    @jobr97 I see nothing. Without a caption, the label will refer to whatever is the last number generated, so the result of \ref would be unpredictable. – egreg Dec 12 '16 at 15:08
  • now It should be visible – jobr97 Dec 12 '16 at 15:20

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.