Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
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 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 '11 at 0:33

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.