20

I'm using minted package. My problem is \usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5} line. I do need this line stretch, but I don't want it to affect code sources generated by minted. How can this be done? Also, I would like to be able to change font size used by code listings independently from rest of document. I think I want 10 or 11pts for code listings and 12pt for rest of the text.

\documentclass[12pt,a4paper]{mwrep}
\usepackage{minted}
\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}
\begin{document}
\begin{minted}{java}
public class Foo
{
  public static void main(String[] args)
  {
    for(int i=0; i<10; i++)
    {
      System.out.println(i);
    }
  }
}
\end{minted}
\end{document}

3 Answers 3

18

You could insert the following code in your document's preamble, after loading the minted package:

\usepackage{etoolbox}
\AtBeginEnvironment{minted}{\singlespacing%
    \fontsize{10}{10}\selectfont}

Alternatively, you could start each minted environment with the following options: baselinestretch=1 and fontsize=\footnotesize. The latter works because if you set the main font size to be 12pt, then \footnotesize will switch to a font size that's 2pt smaller; if you want your code to be typeset in 11pt (and the main font size is still 12pt), you should use the command \small instead of \footnotesize.

8
  • 4
    Or, as it was recommended in another topic, \BeforeBeginEnvironment{minted}{\begin{singlespacing*}} and \AfterEndEnvironment{minted}{\end{singlespacing*}} which may produce better spacing around the environment.
    – egreg
    Nov 21, 2011 at 0:27
  • 1
    @egreg: Your suggestion would seem to be more in conformance with the (slighly terse) explanation given in the setspace package. I suppose which method is "better" for the OP depends largely on how much whitespace the OP would like to get above and below each minted environment.
    – Mico
    Nov 21, 2011 at 0:33
  • @egreg I tried your solution but it seems not to work. Should I load any additional package? (do I need etoolbox?)
    – POliveira
    Apr 5, 2015 at 12:55
  • @POliveira Yes, you need etoolbox
    – egreg
    Apr 5, 2015 at 12:56
  • @egreg Right, I tried your solution and I was given an unknown environment error. As far as I could understand, the environment would be called singlespace instead of singlespacing. Just one more thing, are the asterisks needed at the end of the environment's name? If so, what for? Thanks
    – POliveira
    Apr 5, 2015 at 14:52
31

\setminted{fontsize=\small,baselinestretch=1}

Enjoy

1
  • 7
    I guess this should be the correct answer.
    – asante
    Oct 30, 2018 at 10:14
14

A bit late to the party, but defining an environment such as

\newminted{java}{fontsize=\footnotesize}

works too. Then all

\begin{javacode}...\end{javacode} 

will have font size set to footnotesize.

You must log in to answer this question.

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