1

I am trying to include a python code (within a colored mdframed box) into my tex file. However, I noticed that when I use the geometry package simultaniously with mdframed and fancyhdr, two things happen:

1) automatic line breaking within mdframed boxes doesn't work anymore 2) the top and bottom margins are set to a value around 1cm (no matter what value I set)

When I remove the geometry package, the line breaking works, but the (default) margins are again way too large.

Here my code:

\documentclass[12pt, a4paper, titlepage]{article}

\usepackage[dvipsnames]{xcolor}
\usepackage[ngerman]{babel}

\usepackage[margin=2.54cm]{geometry}
\usepackage{fancyhdr}
\usepackage{mdframed}

\pagestyle{fancy}

\newcommand{\gbx}[1]{
\begin{mdframed}[backgroundcolor=black!20, leftmargin=0, rightmargin=0, innertopmargin=10pt, innerbottommargin=10pt, topline=false, bottomline=false, rightline=false, leftline=false]
#1
\end{mdframed}
}

\begin{document}

\gbx{\texttt{import numpy as np\\
import matplotlib.pyplot as plt\\\\
verteilung = np.random.normal(0., 1., 1000000)\\
bins = len(verteilung)/15000\\\\
fig = plt.figure()\\
ax = fig.add\_subplot(111)\\
plt.hist(verteilung, bins = bins, normed = 1, color = 'blue', label = 'Zufallswerte')\\
plt.xlabel('Wert')\\
plt.ylabel('Anzahl')\\
ax.annotate('Maximum', xy = (0, 0.4), xytext = (-0.75, 0.42))\\
plt.legend()\\
plt.grid()\\
plt.savefig('histo.svg', format='svg', dpi=1000)\\
plt.show()
}}

\end{document}

Now does anyone have a solution for this? How can I get automatic line breaking AND a 2.54cm margin (while using fancyhdr)?

Thanks a lot!

PS: the anysize package (used to set the margins) produces the same output.

5
  • fancyhdr has nothing to do with this. You're using the wrong tool here. You should use listings and a wrapper environment
    – user31729
    Commented Jan 29, 2017 at 13:49
  • The reason that it works without geometry is that the margins just that large that line breaking works within mdframed. That's pure coincidence. Increasing to margin=3cm will also provide linebreaking, but of course, that value is too large for you
    – user31729
    Commented Jan 29, 2017 at 13:54
  • ok. I am also using mdframed for other text, not just code. So how can I get line breaking in that case?
    – Philipp
    Commented Jan 29, 2017 at 13:55
  • The main problem is \texttt - it does not break with that font
    – user31729
    Commented Jan 29, 2017 at 13:56
  • See tex.stackexchange.com/questions/348795/… for (better/another) approach, please
    – user31729
    Commented Jan 29, 2017 at 14:02

0

You must log in to answer this question.

Browse other questions tagged .