如何允许使用小页面或代码块进行分页?

如何允许使用小页面或代码块进行分页?

我想要一个代码块(有两列)可以延续到第一页的空白底部,并且延续到下一页的顶部,怎么做?

我用渲染了两列的代码块minipage,但是代码太长,无法延续到第一页的底部。像MWE一样:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{geometry}

\geometry{a4paper,left=25mm,right=25mm,top=25mm,bottom=25mm, headheight=20mm}
\begin{document}

\lipsum[1-6]

\vspace{2cm}
\begin{minipage}[t]{0.58\textwidth}
\begin{lstlisting}
magic = [[6, 13, 11, 4], [3, 12, 14, 5],
         [16, 7, 1, 10], [9, 2, 8, 15]]
def is_magic(n = 4):
    flag = True
    sums = (1 + n * n) * n // 2
    #############
    for i in range(n):
        s1, s2 = 0, 0
        for j in range(n):
            s1 += magic[i][j]
            s2 += magic[j][i]
        if s1 != sums or s2 != sums:
            flag = False
            break
    if not flag:
        print("NO")
        return
    flag = True
    #########
    s1, s2 = 0, 0
    for i in range(n):
        s1 += magic[i][i]
        s2 += magic[i][n-i-1]
    if s1 != sums or s2 != sums:
        flag = False
    if not flag:
        print("NO")
    else:
        print("YES")
\end{lstlisting}
\end{minipage}
\begin{minipage}[t]{0.41\textwidth}
\begin{lstlisting}
6 13 11 4
3 12 14 5
16 7 1 10
9 2 8 15
OUTPUT:YES

8 6 1
3 5 7
4 9 2
OUTPUT:NO
\end{lstlisting}
\end{minipage}

\end{document}

1 在此处输入图片描述

答案1

将内容封装lstlisting在小页面内使得内容牢不可破,并可避免超出页面边界。

paracol请使用具有两列的包。

b

这是完整的代码。用于\switchcolumn更改列。

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{geometry}
\geometry{a4paper,left=25mm,right=25mm,top=25mm,bottom=25mm, headheight=20mm}

\usepackage{paracol} % added <<<<<<<<<<<<<<<<<<<<<

\begin{document}
    
\lipsum[1-6]
\bigskip
    
\begin{paracol}{2}
\begin{lstlisting}
magic = [[6, 13, 11, 4], [3, 12, 14, 5],
        [16, 7, 1, 10], [9, 2, 8, 15]]
def is_magic(n = 4):
    flag = True
    sums = (1 + n * n) * n // 2
    #############
    for i in range(n):
        s1, s2 = 0, 0
    for j in range(n):
        s1 += magic[i][j]
        s2 += magic[j][i]
    if s1 != sums or s2 != sums:
        flag = False
        break
    if not flag:
        print("NO")
        return
    flag = True
    #########
    s1, s2 = 0, 0
    for i in range(n):
        s1 += magic[i][i]
        s2 += magic[i][n-i-1]
    if s1 != sums or s2 != sums:
        flag = False
    if not flag:
        print("NO")
    else:
        print("YES")
\end{lstlisting}
\switchcolumn % go  to the right column <<<<
\begin{lstlisting}
        6 13 11 4
        3 12 14 5
        16 7 1 10
        9 2 8 15
        OUTPUT: YES
    
        8 6 1
        3 5 7
        4 9 2
        OUTPUT: NO
\end{lstlisting}

\end{paracol}
    
\end{document}

相关内容