4

I know this is likely a simple Latex macro question. I've got a large document with a ton of lstlisting blocks, and I'd like to use minted instead.

That means turning blocks that look like this:

\begin{lstlisting}
  ...code...
\end{lstlisting}

Into something that looks like this:

\begin{minted}{scala}
  ...code...
\end{minted}

I know I can do a find/replace but want an excuse to figure out how to write some of my own LaTeX macros :)

Any suggestions on how to achieve this?

  • 2
    Generally this would be easy but your problem here is going to be that these are verbatim-like environments so you normally need exactly the closing text to be in the source. – Joseph Wright Jul 14 '15 at 15:50
  • And in a certain sense, I wouldn't want to obscure a LaTeX document like that -- there are plenty of other ways to get your hands dirty in macro programming :) – Sean Allred Jul 14 '15 at 16:01
  • 1
    @Sean Allred, I have a several hundred page document that I just want to test out minted on. I don't want to do a find/replace all lstlsting blocks just to look at it afterwards and decide – meh, nevermind. So it seemed like a little macro would help me at least see how minted looks. – Heather Miller Jul 14 '15 at 16:04
  • @JosephWright, I don't follow – I have a big document that already compiles with the closing text in place. Could you elaborate just a bit further? – Heather Miller Jul 14 '15 at 16:06
  • 1
    @HeatherMiller Consider something like sed, then -- basic tool in the kit: sed 's/{lstlsting}/{minted}{scala}/' file.tex > minted-test.tex. Make a backup though, just in case -- I haven't tested that command. What Joseph means is that verbatim environments (like {lstlisting} and {minted}) look for exactly the text \end{lstlisting} (or \end{minted}) to stop the environment. The normal way environments are ended is a bit simpler, so making it a macro isn't a problem. Something like this though would be much more involved, if not infeasible. – Sean Allred Jul 14 '15 at 16:19
5

You can do this with \newminted command

\documentclass{article}
\usepackage{minted}

\newminted[lstlisting]{scala}{}

\begin{document}



\begin{lstlisting}
#include <stdio.h>
#define N 10
/* Block
 * comment */

int main()
{
    int i;

    // Line comment.
    puts("Hello world!");

    for (i = 0; i < N; i++)
    {
        puts("LaTeX is also great for programmers!");
    }

    return 0;
}
\end{lstlisting}

\end{document}
  • Well how about that. I guess this is akin to tex.stackexchange.com/q/31980/17423 after all :) – Sean Allred Jul 14 '15 at 17:42
  • @SeanAllred I am not sure, if i understand this is another thing – touhami Jul 14 '15 at 17:49
  • Well, it's the same idea as \lstnewlisting, but implemented with minted :) (I'm not saying this is a dupe, for the record -- just pointing out the parallels.) – Sean Allred Jul 14 '15 at 17:50
  • @SeanAllred ok i think you're write and that's what i was pointing – touhami Jul 14 '15 at 17:54

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.