8

I'm using minted to format code samples in my document, and I have set a bgcolor option to make them stand out. By default (as in Harish's answer below) the code block will naturally have some margins above and below to separate the code from the paragraphs. However, when I set a bgcolor, that goes away. The question is: how can I get the colored background while also getting normal whitespace above and below the code block? (Question has been edited since I narrowed down the problem to the case where bgcolor is set.)

Sample code:

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

\usepackage{color}
\definecolor{light-gray}{rgb}{0.9,0.9,0.9}
\setminted{bgcolor=light-gray}  % this line causes the problem

\begin{document}
\lipsum*[1] Some text

\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}

Some text \lipsum*[2]
\end{document}

The result: enter image description here

  • Are you placing the minted code in a float or not? – Werner Feb 14 '15 at 1:59
  • No, I didn't. That would move it around to convenient places for TeX, like the bottom or top of the page, right? I think I want to keep my code in line, just make sure it has a little breathing room. – workerjoe Feb 14 '15 at 2:03
6

I believe it's a bug in minted, because the internal minted@colorbg environment, used when the bgcolor option has been given, doesn't add vertical space above and below.

Here's a proposed fix:

\documentclass{article}
\usepackage{minted}
\usepackage{etoolbox}

\usepackage{color}

\usepackage{lipsum}

\definecolor{light-gray}{rgb}{0.9,0.9,0.9}
\setminted{bgcolor=light-gray}  % this line causes the problem

\makeatletter
\patchcmd{\minted@colorbg}{\noindent}{\medskip\noindent}{}{}
\apptocmd{\endminted@colorbg}{\par\medskip}{}{}
\makeatother

\begin{document}
\lipsum*[1] Some text

\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}

Some text \lipsum*[2]
\end{document}

enter image description here

6

I don't know why you want this but as such, minted is separated well enough as in

\documentclass{article}
\usepackage{minted}
\usepackage{lipsum}
\begin{document}
\lipsum*[1] Some text
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
Some text \lipsum*[2]
\end{document}

enter image description here

However, if you insist, here is a way.

\documentclass{article}
\usepackage{minted}
\usepackage{lipsum}
\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}{\bigskip}
\AfterEndEnvironment{minted}{\bigskip}
\begin{document}
\lipsum*[1] Some text
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
Some text \lipsum*[2]
\end{document}

enter image description here

Edit:

Now the MWE is given and the solution still works:

\documentclass{article}
\usepackage{minted}
\usepackage{lipsum}
\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}{\vspace{2cm}}
\AfterEndEnvironment{minted}{\vspace{2cm}}
\usepackage{xcolor}
\definecolor{light-gray}{rgb}{0.9,0.9,0.9}

\begin{document}
\lipsum*[1] Some text

\begin{minted}[bgcolor=light-gray]{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}

Some text \lipsum*[2]
\end{document}

enter image description here

  • Thanks for pointing that out. I was setting a bgcolor option for minted, and it seems to mess with this default behavior. Insert this line into your example and you'll see that it takes away the spacing: \setminted{bgcolor=red} – workerjoe Feb 15 '15 at 19:10
  • @joeclark-phd: How do I know? From where did you get \setminted?. See the edit. – user11232 Feb 15 '15 at 21:25
  • But suppose that you don't want a new paragraph to start after the code block? So Some text \lipsum*[2] would immediately follow \end{minted} without a blank line. When I do this with your code, the formatting gets messed up ("Some" is on the same line as the code and then there's the spacing before the rest of the paragraph). – itzy Feb 26 '16 at 17:46
  • 1
    @itzy: Try \AfterEndEnvironment{minted}{\par\noindent}. – user11232 Mar 27 '16 at 3:17

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.