2

Question

How can I prevent the label / frame to be split from source code in minted?

What I am talking about is this:

enter image description here

MWE

mwe.tex:

\documentclass[a5paper]{scrartcl}
\usepackage{amssymb, amsmath} % needed for math
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout
\usepackage{minted} % needed for the inclusion of source code

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=n-damen.py]{python}{n-damen.py}
\end{document}

n-damen.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def get_next(n, i, damen_pos):
    for i in range(n):
        candidates  = set(list(range(n)))
        candidates -= set(damen_pos)
        candidates -= set(list(range(damen_pos[i]+1)))
        candidates  = list(candidates)
        if len(candidates) > 0:
            damen_pos[i] = candidates[0]
            return i, damen_pos
        else:
            damen_pos = damen_pos[0:i] + [0]*(n-i)
            i -= 1

def is_attacked(damen, x, y):
    """ Wird das Feld (x,y) von einer der Damen angegriffen? """
    for dy, dx in enumerate(damen[:y]):
        if dx == x or dy == y or abs(x-dx) == abs(y-dy):
            return True
    return False

def finde_loesung(n):
    """ Platziere n Damen so auf einem n x n Feld,
        sodass sich keine Damen schlagen. 
    """
    # damen[i] ist die x-position von Dame i in Zeile i
    damen = [0]*n
    i = 1
    solutions = []
    while 0 <= i < n:
        while not is_attacked(damen, damen[i], i):
            if i == n-1:
                yield damen
                break
            i += 1
        i, damen = get_next(n, i, damen)

def alle_loesungen(n):
    generator = finde_loesung(n)
    return list(generator)

print(len(alle_loesungen(11)))

Compile it like this:

pdflatex -shell-escape mwe.tex -output-format=pdf
  • Not related with your question but with your mwe. Do you know package lipsum? – Ignasi Mar 27 '14 at 11:29
  • @Ignasi: Yes, but I always forget how to actually insert lipsum and my editor (sublime) allows me to enter lorem, press tab and get a paragraph. – Martin Thoma Mar 27 '14 at 11:41
2

The minted package is passing typesetting to the fancyvrb package. I guess that the package is missing \penalty\@M in this particular case. It is not easy to get there, but this is the patch.

%! *latex mwe.tex
%   shell escape on
\documentclass[a5paper]{scrartcl}
\usepackage{amssymb, amsmath} % needed for math
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout
\usepackage{minted} % needed for the inclusion of source code

% The patch...
\makeatletter
\def\FV@BeginListFrame@Lines{%
  \begingroup
  \lineskip\z@skip
  \FV@SingleFrameLine{\z@}%
  \kern-0.5\baselineskip\relax
  \baselineskip\z@skip
  \kern\FV@FrameSep\relax
  \penalty\@M% added line
\endgroup}
\makeatother

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=n-damen.py]{python}{n-damen.py}

\end{document}

mwe

2

If you don't want to use Malipivo's patch, a workaround could be using tcolorbox package which can produce all kind of framed and breakable boxes, including listings processed with listings and minted. Your original output could be, more or less, reproduced with

\documentclass[a5paper]{scrartcl}
\usepackage{amssymb, amsmath} % needed for math
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout
\usepackage{minted} % needed for the inclusion of source code
\usepackage[skins,breakable,minted]{tcolorbox}


\newtcbinputlisting{\mintedcode}[2][]{%
    enhanced, breakable, 
    listing engine=minted, minted style=colorful, 
    minted language=python, 
    minted options={linenos, numbersep=5pt, tabsize=4}, 
    listing only, listing file={#2}, label={#2},
    overlay first={\node[font=\ttfamily] at (frame.north) (title) {#2}; \draw[thick] (frame.north west)--(title); \draw[thick](title)--(frame.north east); },
    overlay last={\draw[thick] (frame.south west)--(frame.south east);}, frame hidden,
    #1, opacityback=0, enlarge top by=3mm }


\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

%\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=n-damen.py]{python}{n-damen.py}

\mintedcode{n-damen.py}

\end{document}

enter image description here

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.