如何并排显示两个包含长度不等列表的小页面?

如何并排显示两个包含长度不等列表的小页面?

我有两个列表,它们是我的 Makefile 源。这些 Makefile 中的某些行很长。我需要将它们并排放在报告中的特定位置。我尝试过这样做,但没有成功。

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

\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\graphicspath{{./Figures/}}

\usepackage{url}
%\usepackage{titlesec}
%\newcommand{\sectionbreak}{\clearpage}
\usepackage{dirtree}
\usepackage{color}
\usepackage{listings}
\usepackage{caption}

%to get clickable links from table of contents
\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}
\newcommand{\courierword}[1]{\textsf{\itshape #1}}{\fontfamily{pcr}\selectfont}%

\setlength{\parindent}{0.0cm}
\setlength{\parskip}{1ex}

\setkomafont{sectioning}{\normalcolor\bfseries}

实际内容如下

\begin{figure}[!ht]
\begin{minipage}{.5\textwidth}
\captionof{figure}{Makefile for 'aocs' Collection}
\lstset{language=make,
        commentstyle=\color{red},
        breaklines=true,
}
\begin{lstlisting}[frame=single][t]
This section contains my Makefile 2
Some lines are really toooooooooooooooooooooooooooooooooo long
\end{lstlisting}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\captionof{figure}{Makefile for 'aocsApFw' Constituent}
\lstset{language=make,
        commentstyle=\color{red},
        breaklines=true,
}
\begin{lstlisting}[frame=single][t]
This section contains my Makefile 2
Some lines are really toooooooooooooooooooooooooooooooooo long
\end{lstlisting}
\end{minipage}
\end{figure}

答案1

对您的代码的一些注释:

  1. 您正在使用列表选项frame。因此,您的列表本身会变得更宽。新列表的宽度为:

    framerule+framesep+0.5\linewidth+framesep+framerule
    

    为了解决这个问题,您必须添加以下选项:

    xleftmargin=3.4pt,xrightmargin=3.4pt,
    
  2. 使用选项breaklines=true列表可以拆分为长行。但是拆分仅发生在声明为的字符处other。下表显示了默认定义(请参阅文档) 在此处输入图片描述

  3. 环境minipage有一个可选参数用于调整垂直位置。根据您的情况,您可以使用t。请注意,标题需要相同数量的行。

这是您的代码的修改结果

\documentclass[12pt, a4paper, titlepage]{scrartcl}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\graphicspath{{./Figures/}}

\usepackage{url}
%\usepackage{titlesec}
%\newcommand{\sectionbreak}{\clearpage}
\usepackage{dirtree}
\usepackage{color}
\usepackage{listings}
\usepackage{caption}

%to get clickable links from table of contents
\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}
\newcommand{\courierword}[1]{\textsf{\itshape #1}}{\fontfamily{pcr}\selectfont}%

\setlength{\parindent}{0.0cm}
\setlength{\parskip}{1ex}

\setkomafont{sectioning}{\normalcolor\bfseries}


\begin{document}
\begin{figure}[!ht]
\begin{minipage}[t]{.5\textwidth}
\caption{Makefile for 'aocs' Collection}\par\strut
\lstset{language=make,breakatwhitespace=false,xleftmargin=3.4pt,xrightmargin=3.4pt,
        commentstyle=\color{red},
        breaklines=true,
}
\begin{lstlisting}[frame=single][t]
This section contains my Makefile 2
Some lines are really tooooooooooooooo ooooooooooooooooooo long
\end{lstlisting}
\end{minipage}%
\begin{minipage}[t]{.5\textwidth}
\caption{Makefile for 'aocsApFw' Constituent}
\lstset{language=make,,xrightmargin=3.4pt,,xleftmargin=3.4pt,
        commentstyle=\color{red},
        breaklines=true,
}
\begin{lstlisting}[frame=single][t]
This section contains my Makefile 2
Some lines are really toooooooooooooooo oooooooooooooooooo long
\end{lstlisting}
\end{minipage}
\end{figure}
\end{document}

在此处输入图片描述

相关内容