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}
lockstep
  • 250,273
Ichibann
  • 8,381

3 Answers3

31

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

Enjoy

Himura
  • 903
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.

Mico
  • 506,678
  • 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 '11 at 00: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 '11 at 00:33
  • @egreg I tried your solution but it seems not to work. Should I load any additional package? (do I need etoolbox?) – POliveira Apr 05 '15 at 12:55
  • @POliveira Yes, you need etoolbox – egreg Apr 05 '15 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 05 '15 at 14:52
  • @POliveira Sorry, but I can't follow you. Probably a new question is better. – egreg Apr 05 '15 at 14:55
  • FYI: Still working with the minted version 2.0 package. – larkee Apr 19 '15 at 21:53
  • Note: this may not work with other minted commands/environments e.g. \inputminted. – user202729 Jul 07 '22 at 06:19
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.

Josh
  • 311