Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a figure that consists of multiple paragraphs of tabulars and images. At least one of these paragraphs is wider than the text width. How can I center the entire figure so that the widest paragraph is perfectly centered, and the left edge of all other paragraphs is aligned with the left edge of the widest paragraph?

For example, how can I make the following document

\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}

look like:

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

where . is a space and * is part of the boxed region. I don't know the width of the largest paragraph in advance.

Below is a real example. The goal is to center the entire table at once.

\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}

Note: The answers to similar questions such as How can I center a too wide table? do not work for multiple paragraphs.

share|improve this question
    
Given the difficulties we're having in finding a suitable solution, perhaps it would be best to simply include the troublesome portion as your MWE. For images, check out the mwe package (which includes lipsum). –  Sean Allred Aug 1 '13 at 3:03
    
@SeanAllred I added one of my real examples. –  Ed McMan Aug 1 '13 at 3:39

1 Answer 1

up vote 3 down vote accepted

Update:

The idea is to box the material first and measure the width; this width is used to increase the margins with the help of adjustwidth; the material is then typeset using a varwidth environment.

\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}

enter image description here

And with the code of the updated answer (I made some changes, in particular, changed \bf for \bfseries and \it for \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}

enter image description here

One option using the changepage package and its adjustwidth environment:

\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}

enter image description here

share|improve this answer
    
Per the Q's edit---does this work with the tabular environment? EDIT: ... and images? (IIRC, it should) –  Sean Allred Aug 1 '13 at 1:54
    
Just edited my question to make it clear that the paragraphs have rigid widths. This causes the paragraphs to not be centered if the lengths passed to adjustwidth are too big. –  Ed McMan Aug 1 '13 at 1:57
    
@EdMcMan and how exactly are you producing your rigid width paragraphs? –  Gonzalo Medina Aug 1 '13 at 1:58
1  
@SeanAllred adjustwidth internally uses a list, so images and tabular material won't be a problem here. –  Gonzalo Medina Aug 1 '13 at 1:59
    
@GonzaloMedina There are tabulars such as \begin{tabular}{lllp{4in}}. These don't expand to use extra margin space. –  Ed McMan Aug 1 '13 at 2:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.