# [Tex/LaTex] Justifying (‘ragged right/left, center’) all the tables in a document

horizontal alignmenttables

Suppose I want to left justify all my tables to a certain vertical line (MWE):

\documentclass[11pt,a4paper,oneside]{memoir}

\pagestyle{plain}

\usepackage{fontspec}
%\usepackage{changepage}

\setmainfont[Mapping=tex-text]{Liberation Serif}
\setsansfont[Mapping=tex-text]{Liberation Sans}
\setmonofont[Mapping=tex-text]{Liberation Mono}

\begin{document}

\chapter{}

All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae.

\raggedright{Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}

\hline
{Alfa}
& {Bravo}
\\\hline

{Charlie}
& {Delta}
\\\hline

\end{tabular}

\bigskip
Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind.

\raggedright{Another Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}

\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline

{Golf}
& {Hotel}
\\\hline

\end{tabular}

\bigskip
They are the nearest to the Germans, who dwell beyond the Rhine, with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor.

\end{document}


This solution works, but I want more flexibility. How, for instance, am I to redefine \leftpadding to center all the tables at once? It should be possible, at least in two passes. The option of right justifying wouldn't hurt, too. Entirely different solutions to the same purpose are also welcome.

Though I don't quite get what you want, here is a solution (which hopefully fits your needs) using the pgfkeys-package. I made the captions in a way, that they can be labeled (using the label= key) but they don't use the \caption command internally. Also it doesn't use your command \leftpadding but the padding-key.

\documentclass[11pt,a4paper,oneside]{memoir}

\pagestyle{plain}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage{fontspec}
%\usepackage{changepage}

%\setmainfont[Mapping=tex-text]{Liberation Serif}
%\setsansfont[Mapping=tex-text]{Liberation Sans}
%\setmonofont[Mapping=tex-text]{Liberation Mono}

\usepackage{pgfkeys}
\makeatletter
\newif\if@placedleft
\newif\if@placedright
\newif\if@placedmiddle
\newif\if@placedcapused
\newif\if@placedscapused
\newif\if@placedlabused
\newdimen\@placedrest
\pgfkeys{/placedt/.is family,/placedt,
default/.style={% change the default settings here
place=left,
stretch=1.4,
},
stretch/.estore in=\arraystretch,
place/.is choice,
place/left/.code=\@placedlefttrue\@placedrightfalse\@placedmiddlefalse,
place/right/.code=\@placedrighttrue\@placedleftfalse\@placedmiddlefalse,
place/middle/.code=\@placedmiddletrue\@placedleftfalse\@placedrightfalse,
caption/.code=\@placedcapusedtrue\def\@placedcap{#1},
scaption/.code=\@placedscapusedtrue\def\@placedscap{#1},
label/.code=\@placedlabusedtrue\def\@placedlab{#1},
}

\newenvironment{placedtabular}[2][]{%
\par\medskip\noindent\begin{minipage}{\textwidth}%
\pgfkeys{/placedt, default, #1}%
\if@placedleft%
\@placedrest=\textwidth%
\begin{minipage}{\@placedrest}%
\raggedright%
\else\if@placedmiddle%
\centering%
\else\if@placedright%
\@placedrest=\textwidth%
\begin{minipage}{\@placedrest}%
\raggedleft%
\fi\fi\fi%
\if@placedcapused%
\refstepcounter{table}%
\tablename~\thetable: \@placedcap%
\if@placedscapused%
\else%
\fi%
\if@placedlabused%
\label{\@placedlab}%
\fi%
\\[0.2\baselineskip]%
\fi%
\footnotesize\begin{tabular}{#2}%
}{
\end{tabular}%
\if@placedmiddle\else%
\end{minipage}%
\fi%
\end{minipage}%
}
\makeatother

\begin{document}
\listoftables

\chapter{}

All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae.

\begin{placedtabular}[caption=Caption,label=tab:first]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline

{Charlie}
& {Delta}
\\\hline
\end{placedtabular}

\raggedright{Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}

\hline
{Alfa}
& {Bravo}
\\\hline

{Charlie}
& {Delta}
\\\hline

\end{tabular}

\bigskip
Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind.

\begin{placedtabular}[caption=Another Caption,label=tab:second]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline

{Golf}
& {Hotel}
\\\hline
\end{placedtabular}
\raggedright{Another Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}

\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline

{Golf}
& {Hotel}
\\\hline

\end{tabular}

\bigskip
They are the nearest to the Germans, who dwell beyond the Rhine, with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor.

\end{document}


EDIT: Note that I have slightly altered the above code.

Usage of the placedtabular-environment is just like a normal tabular except, that in the optional argument you can use a comma seperated key=value syntax. The following keys are alailable:

• stretch: the value used for arraystretch
• place: the placement of the table. Available are left, right and middle
• padding: the space to the left or right margin with place=left or place=right, respectively
• caption: the caption used for the table
• scaption: the table-name for the \listoftables, if not used caption is used. If caption is not used, this doesn't have any effect.
• label: the label used for referencing the table

When you use one of these keys in a placedtabular-environment, the effects are only local and don't affect any other table set with this environment.

From the aforementioned keys place, stretch and padding have a default value, which are set in between the brackets of default/.style={...} which is used inside the first call of \pgfkeys in the above MWE. There is also the comment 'change the default settings here'.

EDIT2: The below code is everything inbetween of \makeatletter and \makeatother and doesn't set 'table x.y' infront of the caption. Because of this, I also removed the label-key as referencing won't work with this.

\makeatletter
\newif\if@placedleft
\newif\if@placedright
\newif\if@placedmiddle
\newif\if@placedcapused
\newif\if@placedscapused
%\newif\if@placedlabused
\newdimen\@placedrest
\pgfkeys{/placedt/.is family,/placedt,
default/.style={%
place=left,
stretch=1.4,
},
stretch/.estore in=\arraystretch,
place/.is choice,
place/left/.code=\@placedlefttrue\@placedrightfalse\@placedmiddlefalse,
place/right/.code=\@placedrighttrue\@placedleftfalse\@placedmiddlefalse,
place/middle/.code=\@placedmiddletrue\@placedleftfalse\@placedrightfalse,
caption/.code=\@placedcapusedtrue\def\@placedcap{#1},
scaption/.code=\@placedscapusedtrue\def\@placedscap{#1},
}

\newenvironment{placedtabular}[2][]{%
\par\medskip\noindent\begin{minipage}{\textwidth}%
\pgfkeys{/placedt, default, #1}%
\if@placedleft%
\@placedrest=\textwidth%
\begin{minipage}{\@placedrest}%
\raggedright%
\else\if@placedmiddle%
\centering%
\else\if@placedright%
\@placedrest=\textwidth%
\begin{minipage}{\@placedrest}%
\raggedleft%
\fi\fi\fi%
\if@placedcapused%
\@placedcap%
\if@placedscapused%
\else%
\fi%
\\[0.2\baselineskip]%
\fi%
\footnotesize\begin{tabular}{#2}%
}{
\end{tabular}%
\if@placedmiddle\else%
\end{minipage}%
\fi%
\end{minipage}%
}


\makeatother

EDIT3: Complete code (without label and without the Table x.y: prefix) which includes two more options:

• font are fontswitches applied to the table (stuff like \bfseries, \footnotesize or \small)
• captionfont are fontswitches applied to the caption

Both have the default to no change to surrounding fontswitches. If used both reset size and font and apply the argument provided.

\documentclass[11pt,a4paper,oneside]{memoir}

\pagestyle{plain}

\usepackage{fontspec}
%\usepackage{changepage}

\setmainfont[Mapping=tex-text]{Liberation Serif}
\setsansfont[Mapping=tex-text]{Liberation Sans}
\setmonofont[Mapping=tex-text]{Liberation Mono}

\usepackage{pgfkeys}
\makeatletter
\newif\if@placedleft
\newif\if@placedright
\newif\if@placedmiddle
\newif\if@placedcapused
\newif\if@placedscapused
\newif\if@placedfontused
\newif\if@placedcfontused
\newdimen\@placedrest
\pgfkeys{/placedt/.is family,/placedt,
default/.style={%
place=left,
stretch=1.4,
},
stretch/.estore in=\arraystretch,
place/.is choice,
place/left/.code=\@placedlefttrue\@placedrightfalse\@placedmiddlefalse,
place/right/.code=\@placedrighttrue\@placedleftfalse\@placedmiddlefalse,
place/middle/.code=\@placedmiddletrue\@placedleftfalse\@placedrightfalse,
caption/.code=\@placedcapusedtrue\def\@placedcap{#1},
scaption/.code=\@placedscapusedtrue\def\@placedscap{#1},
font/.store in=\@placedfont,
captionfont/.store in=\@placedcapfont,
font/.code=\@placedfontusedtrue\def\@placedfont{#1},
captionfont/.code=\@placedcfontusedtrue\def\@placedcapfont{#1},
}

\newenvironment{placedtabular}[2][]{%
\par\medskip\noindent\begin{minipage}{\textwidth}%
\pgfkeys{/placedt, default, #1}%
\if@placedleft%
\@placedrest=\textwidth%
\begin{minipage}{\@placedrest}%
\raggedright%
\else\if@placedmiddle%
\centering%
\else\if@placedright%
\@placedrest=\textwidth%
\begin{minipage}{\@placedrest}%
\raggedleft%
\fi\fi\fi%
\if@placedcapused%
{\if@placedcfontused\normalsize\normalfont\@placedcapfont\fi\@placedcap}%
\if@placedscapused%
\else%
\fi%
\\[0.2\baselineskip]%
\fi%
\if@placedfontused\normalsize\normalfont\@placedfont\fi%
\begin{tabular}{#2}%
}{
\end{tabular}%
\if@placedmiddle\else%
\end{minipage}%
\fi%
\end{minipage}%
}
\makeatother

\begin{document}
\listoftables

\chapter{}

All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae.

\begin{placedtabular}[caption=Caption,font=\bfseries]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline

{Charlie}
& {Delta}
\\\hline
\end{placedtabular}

\raggedright{Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}

\hline
{Alfa}
& {Bravo}
\\\hline

{Charlie}
& {Delta}
\\\hline

\end{tabular}

\bigskip
Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind.

\begin{placedtabular}[caption={Another\protect\\Caption}]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline

{Golf}
& {Hotel}
\\\hline
\end{placedtabular}
\begin{placedtabular}[caption=Another Caption]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline

{Golf}
& {Hotel}
\\\hline
\end{placedtabular}
\raggedright{Another Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}

\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline

{Golf}
& {Hotel}
\\\hline

\end{tabular}