如何使宽度大于文本宽度的多个段落居中?

如何使宽度大于文本宽度的多个段落居中?

我有一个由多段表格和图片组成的图形。其中至少有一个段落比文本宽度宽。如何将整个图形居中,以便最宽的段落完全居中,并且所有其他段落的左边缘与最宽段落的左边缘对齐?

例如,如何制作以下文档

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\fbox{\begin{tabular}{lllp{4in}}
a & b & c & \lipsum[4]
\end{tabular}}

\fbox{\begin{tabular}{lllp{3in}}
aaaa & bbbb & cccc & \lipsum[4]
\end{tabular}}

\end{document}

看起来像:

...**********************************************...
...******************************************.......

其中.是空格,*是方框区域的一部分。我事先不知道最大段落的宽度。

下面是一个真实的例子。目标是一次性将整个表格居中。

\documentclass{article}
\usepackage{amssymb}

\begin{document}

\begin{table}
{\bf Contexts}\\
\begin{tabular}{lllp{4in}}
  {\it Statement} & $\Pi$ & $n$ $\mapsto$ \emph{instr} &
  Maps an statement address to an statement.\\
  {\it Variable} & $\Delta$ &  $id \mapsto \emph{var}$  &Maps
  a variable ID to its value.\\
  {\it Labels} & $\Lambda$ & $\emph{label\_kind} \mapsto n$ & Maps
  a label to the address of the corresponding {\tt label} statement number.
\end{tabular}\\
\newline
{\bf Notation}\\
\begin{tabular}{lp{4.2in}}
  $\Delta \vdash e \Downarrow v$ & Expression $e$ evaluates to value
  $v$ given variable context $\Delta$ as given by the expression
  evaluation rules.\\
  $\Delta' = \Delta[x \leftarrow v]$ & $\Delta'$ is the same as
  $\Delta$ except extended to map $x$  to $v$.\\
  $\Pi \vdash p:s$ & $\Pi$ maps statement address $p$ to statement
  $s$. If $p \notin \Pi$, the machine gets stuck.\\
  $\Lambda \vdash v:p$ & $\Lambda$ maps statement label $v$ to statement
  address $p$. If $v \notin \Lambda$, then machine gets stuck. In
  addition, a well-formed machine should have $\Pi \vdash p : s$
  where $s = \texttt{label $v$}$, otherwise the machine is stuck.\
  $(\Delta, p, s) \leadsto (\Delta', p', s')$ &  An
  execution step. $p$ and $p'$ are
  the pre and post step program counters, $s$ and $s'$ are the pre and
  post step  statements, and $\Delta$ and $\Delta'$ are the pre and post
  step variable contexts. Note $\Lambda$ and $\Pi$ are currently static, thus
  for brevity not included in the execution context.\\
\end{tabular}
\caption{Operational Semantics Notation.}
\end{table}

\end{document}

注意:类似问题的答案,例如如何让太宽的表格居中?不适用于多个段落。

答案1

更新:

这个想法是先将材料装箱并测量宽度;借助这个宽度来增加边距adjustwidth;然后使用环境对材料进行排版varwidth

\documentclass{article}
\usepackage{changepage}
\usepackage{varwidth}
\usepackage{lipsum}

\newsavebox\mybox
\newlength\BoxWd

\newcommand\CenterBox[1]{%
\begin{lrbox}{\mybox}
\begin{varwidth}{\paperwidth}
#1
\end{varwidth}%
\end{lrbox}%
\settowidth\BoxWd{\usebox\mybox}%
\addtolength\BoxWd{-\textwidth}%
\begin{adjustwidth}{-0.5\BoxWd}{-0.5\BoxWd}
\usebox\mybox
\end{adjustwidth}%
}

\begin{document}

\lipsum[4]
\CenterBox{\fbox{\begin{tabular}{lllp{5in}}
a & b & c & \lipsum[4]
\end{tabular}}

\fbox{\begin{tabular}{lllp{3in}}
aaaa & bbbb & cccc & \lipsum[4]
\end{tabular}}
}
\lipsum[4]

\end{document}

在此处输入图片描述

并使用更新后的答案的代码(我做了一些更改,特别是\bf\bfseries\it进行了更改\itshape):

\documentclass{article}
\usepackage{changepage}
\usepackage{amssymb}
\usepackage{array}
\usepackage{varwidth}

\usepackage{lipsum}

\newsavebox\mybox
\newlength\BoxWd

\newcommand\CenterBox[1]{%
\begin{lrbox}{\mybox}
\begin{varwidth}{\paperwidth}
#1
\end{varwidth}%
\end{lrbox}%
\settowidth\BoxWd{\usebox\mybox}%
\addtolength\BoxWd{-\textwidth}%
\begin{adjustwidth}{-0.5\BoxWd}{-0.5\BoxWd}
\usebox\mybox
\end{adjustwidth}%
}

\begin{document}

\lipsum[4]
\begin{table}
\CenterBox{%
{\bfseries Contexts}\par
\begin{tabular}{>{\itshape}l>{$}l<{$}lp{4in}}
  Statement & \Pi & $n$ $\mapsto$ \emph{instr} &
  Maps an statement address to an statement.\\
  Variable & \Delta &  $id \mapsto \emph{var}$  &Maps
  a variable ID to its value.\\
  Labels & \Lambda & $\emph{label\_kind} \mapsto n$ & Maps
  a label to the address of the corresponding {\tt label} statement number.
\end{tabular}\par\smallskip
{\bfseries Notation}\par
\begin{tabular}{>{$}l<{$}p{4.2in}}
  \Delta \vdash e \Downarrow v & Expression $e$ evaluates to value
  $v$ given variable context $\Delta$ as given by the expression
  evaluation rules.\\
  \Delta' = \Delta[x \leftarrow v] & $\Delta'$ is the same as
  $\Delta$ except extended to map $x$  to $v$.\\
  \Pi \vdash p:s & $\Pi$ maps statement address $p$ to statement
  $s$. If $p \notin \Pi$, the machine gets stuck.\\
  \Lambda \vdash v:p & $\Lambda$ maps statement label $v$ to statement
  address $p$. If $v \notin \Lambda$, then machine gets stuck. In
  addition, a well-formed machine should have $\Pi \vdash p : s$
  where $s = \texttt{label $v$}$, otherwise the machine is stuck.\\
  (\Delta, p, s) \leadsto (\Delta', p', s') &  An
  execution step. $p$ and $p'$ are
  the pre and post step program counters, $s$ and $s'$ are the pre and
  post step  statements, and $\Delta$ and $\Delta'$ are the pre and post
  step variable contexts. Note $\Lambda$ and $\Pi$ are currently static, thus
  for brevity not included in the execution context.\\
\end{tabular}
\caption{Operational Semantics Notation.}}
\end{table}
\lipsum[4]

\end{document}

在此处输入图片描述

一个选项是使用changepage包及其adjustwidth环境:

\documentclass{article}
\usepackage{changepage}
\usepackage{lipsum}

\begin{document}

\lipsum[4]
\begin{adjustwidth}{-2cm}{-2cm}
\centering
\fbox{\begin{tabular}{lllp{4in}}
a & b & c & \lipsum[4]
\end{tabular}}

\fbox{\begin{tabular}{lllp{3in}}
aaaa & bbbb & cccc & \lipsum[4]
\end{tabular}}
\end{adjustwidth}
\lipsum[4]

\end{document}

在此处输入图片描述

相关内容