如何使环境(表格、多列等)贴近内边距而不是左边距?

如何使环境(表格、多列等)贴近内边距而不是左边距?

我目前正在读LaTeX:文档准备系统,第二版作者 Leslie Lamport,为了学习目的,我尝试重新创建书中存在的某些结构。

例如,在书中,作者使用两列部分来显示代码(右列)和代码的输出(左列)。 下面是一个示例的屏幕截图: 双柱部分

问题是,在使用文档类时,我无法重新创建相同的结构(我multicol为此使用了包,环境位于右列) ,因为在偶数页上,两列部分紧贴左边距。当右列中的代码长度超过一半时,它会打印在内边距内(如下例所示,带有黑色方块的线条)。 verbatimbook\textwidth双列部分的边距不好

tabular对于长度超过的表格(用 制作),也会发生相同的情况\textwidth(见下面的示例)。 表格边距不好

在所有这些情况下,我都希望让这些环境紧贴内边距(偶数页的右边距和奇数页的左边距),并将剩余部分打印在外边距中。当然,这应该只在环境的宽度大于时才会发生\textwidth

编辑:添加了一个最小的工作示例。

\documentclass[12pt,a4paper]{book}

\usepackage{blindtext}
\usepackage{multicol}

\begin{document}

\blindtext

\begin{multicols}{2}\scriptsize
  \noindent
  An intra-word dash or hyphen, as in X-ray.\\
  A medium dash (en-dash) for number ranges, like 1--2.\\
  A punctuation dash---like this (also know as em-dash).\\
  \columnbreak
\begin{verbatim}
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
\end{verbatim}
\end{multicols}

\blindtext

\begin{center}
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{| l | l | l | l | l | l |}
    \hline
    \textit{cmd}   & \textit{decl}   & \textit{cmd}   & \textit{decl}    & \textit{cmd}   & \textit{decl}    \\
    \hline
    \verb|\textup| & \verb|\upshape| & \verb|\textsc| & \verb|\scshape|  & \verb|\textrm| & \verb|\rmfamily| \\
    \verb|\textit| & \verb|\itshape| & \verb|\textmd| & \verb|\mdseries| & \verb|\textsf| & \verb|\sffamily| \\
    \verb|\textsl| & \verb|\slshape| & \verb|\textbf| & \verb|\bfseries| & \verb|\texttt| & \verb|\ttfamily| \\
    \hline
  \end{tabular}
\end{center}

\blindtext

\begin{multicols}{2}\scriptsize
  \noindent
  An intra-word dash or hyphen, as in X-ray.\\
  A medium dash (en-dash) for number ranges, like 1--2.\\
  A punctuation dash---like this (also know as em-dash).\\
  \columnbreak
\begin{verbatim}
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
\end{verbatim}
\end{multicols}

\blindtext

\begin{center}
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{| l | l | l | l | l | l |}
    \hline
    \textit{cmd}   & \textit{decl}   & \textit{cmd}   & \textit{decl}    & \textit{cmd}   & \textit{decl}    \\
    \hline
    \verb|\textup| & \verb|\upshape| & \verb|\textsc| & \verb|\scshape|  & \verb|\textrm| & \verb|\rmfamily| \\
    \verb|\textit| & \verb|\itshape| & \verb|\textmd| & \verb|\mdseries| & \verb|\textsf| & \verb|\sffamily| \\
    \verb|\textsl| & \verb|\slshape| & \verb|\textbf| & \verb|\bfseries| & \verb|\texttt| & \verb|\ttfamily| \\
    \hline
  \end{tabular}
\end{center}

\blindtext

\end{document}

答案1

这使用 paracol 来减少列宽,而不是故意在其他地方超出列宽。您还会注意到,这种方法与标题的对齐方式与示例相同。

有趣的是,没有逐字逐句的多列文字会忽略\columnbreak,但简单的段落分隔符就可以达到目的。

\documentclass[12pt,a4paper]{book}
\usepackage[textwidth=6in,showframe]{geometry}
\usepackage{blindtext}
\usepackage{multicol}
\usepackage{paracol}
\twosided
\setcolumnwidth{0.9\textwidth}

\begin{document}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{multicols}{2}\scriptsize
  \noindent
  An intra-word dash or hyphen, as in X-ray.\\
  A medium dash (en-dash) for number ranges, like 1--2.\\
  A punctuation dash---like this (also know as em-dash).

\noindent 
An intra-word dash or hyphen, as in X-ray.\\
A medium dash (en-dash) for number ranges, like 1--2.\\
A punctuation dash---like this (also know as em-dash).
\end{multicols}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{center}
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{| l | l | l | l | l | l |}
    \hline
    \textit{cmd}   & \textit{decl}   & \textit{cmd}   & \textit{decl}    & \textit{cmd}   & \textit{decl}    \\
    \hline
    \verb|\textup| & \verb|\upshape| & \verb|\textsc| & \verb|\scshape|  & \verb|\textrm| & \verb|\rmfamily| \\
    \verb|\textit| & \verb|\itshape| & \verb|\textmd| & \verb|\mdseries| & \verb|\textsf| & \verb|\sffamily| \\
    \verb|\textsl| & \verb|\slshape| & \verb|\textbf| & \verb|\bfseries| & \verb|\texttt| & \verb|\ttfamily| \\
    \hline
  \end{tabular}
\end{center}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{multicols}{2}\scriptsize
  \noindent
  An intra-word dash or hyphen, as in X-ray.\\
  A medium dash (en-dash) for number ranges, like 1--2.\\
  A punctuation dash---like this (also know as em-dash).

\noindent 
An intra-word dash or hyphen, as in X-ray.\\
A medium dash (en-dash) for number ranges, like 1--2.\\
A punctuation dash---like this (also know as em-dash).
\end{multicols}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{center}
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{| l | l | l | l | l | l |}
    \hline
    \textit{cmd}   & \textit{decl}   & \textit{cmd}   & \textit{decl}    & \textit{cmd}   & \textit{decl}    \\
    \hline
    \verb|\textup| & \verb|\upshape| & \verb|\textsc| & \verb|\scshape|  & \verb|\textrm| & \verb|\rmfamily| \\
    \verb|\textit| & \verb|\itshape| & \verb|\textmd| & \verb|\mdseries| & \verb|\textsf| & \verb|\sffamily| \\
    \verb|\textsl| & \verb|\slshape| & \verb|\textbf| & \verb|\bfseries| & \verb|\texttt| & \verb|\ttfamily| \\
    \hline
  \end{tabular}
\end{center}

\begin{paracol}{2}
\blindtext
\end{paracol}

\end{document}

我突然想到提供翻译是 paracol 的主要用途。我创建了一个新环境 (codecols) 来处理所需的所有参数更改。

\documentclass[12pt,a4paper]{book}
\usepackage[textwidth=6.5in,showframe]{geometry}
\usepackage{blindtext}
%\usepackage{multicol}
\usepackage{paracol}
\twosided
\setcolumnwidth{0.8\textwidth}

\makeatletter
\newenvironment{codecols}{\par\bigskip
  \parindent=0pt
  \@minipagetrue% remove spacing arund \verbatim
  \scriptsize
  \setcolumnwidth{\dimexpr 0.5\textwidth-0.5\columnsep}%
  \twosided[pmb]% do not swap columns
  \begin{paracol}{2}}%
{\end{paracol}
  \twosided
  \setcolumnwidth{0.8\textwidth}}
\makeatother

\begin{document}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{codecols}
  An intra-word dash or hyphen, as in X-ray.\\
  A medium dash (en-dash) for number ranges, like 1--2.\\
  A punctuation dash---like this (also know as em-dash).
\switchcolumn
\begin{verbatim}
An intra-word dash or hyphen, as in X-ray.\\
A medium dash (en-dash) for number ranges, like 1--2.\\
A punctuation dash---like this (also know as em-dash).
\end{verbatim}
\end{codecols}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{center}
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{| l | l | l | l | l | l |}
    \hline
    \textit{cmd}   & \textit{decl}   & \textit{cmd}   & \textit{decl}    & \textit{cmd}   & \textit{decl}    \\
    \hline
    \verb|\textup| & \verb|\upshape| & \verb|\textsc| & \verb|\scshape|  & \verb|\textrm| & \verb|\rmfamily| \\
    \verb|\textit| & \verb|\itshape| & \verb|\textmd| & \verb|\mdseries| & \verb|\textsf| & \verb|\sffamily| \\
    \verb|\textsl| & \verb|\slshape| & \verb|\textbf| & \verb|\bfseries| & \verb|\texttt| & \verb|\ttfamily| \\
    \hline
  \end{tabular}
\end{center}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{codecols}
  An intra-word dash or hyphen, as in X-ray.\\
  A medium dash (en-dash) for number ranges, like 1--2.\\
  A punctuation dash---like this (also know as em-dash).
\switchcolumn
\begin{verbatim}
An intra-word dash or hyphen, as in X-ray.\\
A medium dash (en-dash) for number ranges, like 1--2.\\
A punctuation dash---like this (also know as em-dash).
\end{verbatim}
\end{codecols}

\begin{paracol}{2}
\blindtext
\end{paracol}

\begin{center}
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{| l | l | l | l | l | l |}
    \hline
    \textit{cmd}   & \textit{decl}   & \textit{cmd}   & \textit{decl}    & \textit{cmd}   & \textit{decl}    \\
    \hline
    \verb|\textup| & \verb|\upshape| & \verb|\textsc| & \verb|\scshape|  & \verb|\textrm| & \verb|\rmfamily| \\
    \verb|\textit| & \verb|\itshape| & \verb|\textmd| & \verb|\mdseries| & \verb|\textsf| & \verb|\sffamily| \\
    \verb|\textsl| & \verb|\slshape| & \verb|\textbf| & \verb|\bfseries| & \verb|\texttt| & \verb|\ttfamily| \\
    \hline
  \end{tabular}
\end{center}

\begin{paracol}{2}
\blindtext
\end{paracol}

\end{document}

相关内容