5

I need a general solution for vertical align in tables. That is, a solution for texts and subtables inside the cells.

I try defining new column types like,

\newcolumntype{?}[1]{>{\raggedright\arraybackslash}?{#1}}

But is insuficient because can't predetermine the correct position of the content inside the cell.

Show the variation of the vertical align using only texts

Even worse if the contents of the cell is a table. The vertical alignment becomes blurred and unpredictable.

Vertical align with tables inside the cell

If vertical alignment is used within the subtables (like \begin{tabular}[t], the solution is not complete.

Using vertical align in the table

Therefore, I do not find a universal solution for vertically and horizontally aligning any content in a cell, in a predictable and reliable way.

Please, I need help for this purpose. The contents of the cells can be a text (multiline); a table; or mixed together.

The code used for the examples is as follows:

\documentclass{article}
\usepackage{array}

% Middle alignment
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}
% Top alignment
\newcolumntype{T}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{U}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{V}[1]{>{\raggedleft\arraybackslash}p{#1}}
% Bottom alignment
\newcolumntype{E}[1]{>{\raggedright\arraybackslash}b{#1}}
\newcolumntype{F}[1]{>{\centering\arraybackslash}b{#1}}
\newcolumntype{G}[1]{>{\raggedleft\arraybackslash}b{#1}}

\newcommand{\subtableshort}{\begin{tabular}{ll}a&b\\c&d\end{tabular}}
\newcommand{\subtablecontrol}{\begin{tabular}{ll}a&b\\c&d\\e&f\\g&h\end{tabular}}
\newcommand{\subtableAlignedShort}[1]{\begin{tabular}[#1]{ll}a&b\\c&d\end{tabular}}
\newcommand{\subtableAlignedControl}[1]{\begin{tabular}[#1]{ll}a&b\\c&d\\e&f\\g&h\end{tabular}}

\newcommand{\txtshort}{Donec aliquet, erat ligula aliquet.\newline Suspendisse ut. }
\newcommand{\txtcontrol}{Nam dui ligula, fringilla euismod. \newline Morbi ac orci et nisl hendrerit mollis. Cras ante.}

\newcommand{\textExample}{\hline\txtcontrol&\txtshort&\txtshort&\txtshort\\[0.3in]\hline}
\newcommand{\tableExample}{\hline\subtablecontrol&\subtableshort&\subtableshort&\subtableshort\\\hline}
\newcommand{\alignedTableExample}[1]{\hline\subtableAlignedControl{#1}&\subtableAlignedShort{#1}&\subtableAlignedShort{#1}&\subtableAlignedShort{#1}\\\hline}

\begin{document}

\section{Example with texts}

Using
\begin{verbatim}
\newcolumntype{?}[1]{>{\raggedright\arraybackslash}?{#1}}
\end{verbatim}


\subsection{Middle}
\noindent\begin{tabular}{|L{1.2in}|L{1.2in}|C{1.2in}|R{1.2in}|}\textExample\end{tabular}
\subsection{Top   }
\noindent\begin{tabular}{|T{1.2in}|T{1.2in}|U{1.2in}|V{1.2in}|}\textExample\end{tabular}
\subsection{Bottom}
\noindent\begin{tabular}{|E{1.2in}|E{1.2in}|F{1.2in}|G{1.2in}|}\textExample\end{tabular}
\subsection{Mixed}
\noindent\begin{tabular}{|E{1.2in}|L{1.2in}|T{1.2in}|E{1.2in}|}\textExample\end{tabular}

\section{Example with subtables}
\subsection{Middle}
\noindent\begin{tabular}{|L{1.2in}|L{1.2in}|C{1.2in}|R{1.2in}|}\tableExample\end{tabular}
\subsection{Top}
\noindent\begin{tabular}{|T{1.2in}|T{1.2in}|U{1.2in}|V{1.2in}|}\tableExample\end{tabular}
\subsection{Bottom}
\noindent\begin{tabular}{|E{1.2in}|E{1.2in}|F{1.2in}|G{1.2in}|}\tableExample\end{tabular}

\section{Example with aligned top subtables}
\subsection{Middle}
\noindent\begin{tabular}{|L{1.2in}|L{1.2in}|C{1.2in}|R{1.2in}|}\alignedTableExample{t}\end{tabular}
\subsection{Top}
\noindent\begin{tabular}{|T{1.2in}|T{1.2in}|U{1.2in}|V{1.2in}|}\alignedTableExample{t}\end{tabular}
\subsection{Bottom}
\noindent\begin{tabular}{|E{1.2in}|E{1.2in}|F{1.2in}|G{1.2in}|}\alignedTableExample{t}\end{tabular}

\section{Example with aligned bottom subtables}
\subsection{Middle}
\noindent\begin{tabular}{|L{1.2in}|L{1.2in}|C{1.2in}|R{1.2in}|}\alignedTableExample{b}\end{tabular}
\subsection{Top}
\noindent\begin{tabular}{|T{1.2in}|T{1.2in}|U{1.2in}|V{1.2in}|}\alignedTableExample{b}\end{tabular}
\subsection{Bottom}
\noindent\begin{tabular}{|E{1.2in}|E{1.2in}|F{1.2in}|G{1.2in}|}\alignedTableExample{b}\end{tabular}

\end{document}
1

1 Answer 1

4

Hope this help. Note that m columns do not care of [t] or [b] options in tabulars (and that [t] and [b] options do not care if the column is p or b).

I almost forgot it: the problem with texts is the [0.3in].

mwe

\documentclass[a4paper]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{array}
\setlength\parskip{1ex}

% Middle alignment
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}
% Top alignment
\newcolumntype{T}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{U}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{V}[1]{>{\raggedleft\arraybackslash}p{#1}}
% Bottom alignment
\newcolumntype{E}[1]{>{\raggedright\arraybackslash}b{#1}}
\newcolumntype{F}[1]{>{\centering\arraybackslash}b{#1}}
\newcolumntype{G}[1]{>{\raggedleft\arraybackslash}b{#1}}
% less typing
\newcommand{\txtcontrol}{Nam dui ligula, fringilla euismod. \newline Morbi ac orci et nisl hendrerit mollis. Cras ante.}
\newcommand{\txtshort}{Donec aliquet, erat ligula aliquet. \newline Suspendisse ut. }
\newcommand\TBLC{\hline \txtcontrol & \txtshort & \txtshort &   \txtshort \\\hline}

\begin{document}

\begin{tabular}{p{3cm}p{3cm}p{3cm}p{3cm}} Left & Left &\hfil Center &\hfill Right \\ \end{tabular}\par
\begin{tabular}{|L{3cm}|L{3cm}|C{3cm}|R{3cm}|} \TBLC \end{tabular}\qquad{\huge Medium}\par 
\begin{tabular}{|T{3cm}|T{3cm}|U{3cm}|V{3cm}|} \TBLC \end{tabular}\qquad{\huge Top}\par 
\begin{tabular}{|E{3cm}|E{3cm}|F{3cm}|G{3cm}|} \TBLC \end{tabular}\qquad{\huge Bottom}\par 

%SUBTABLES
\renewcommand{\txtcontrol}{\begin{tabular}{ll} 11 & 12\\ 21 & 22\\ 31 & 32\\ 41 & 42\\\end{tabular}}
\renewcommand{\txtshort}{\begin{tabular}{ll} 11 & 12\\ 21 & 22\\\end{tabular}}

\begin{tabular}{|L{3cm}|L{3cm}|C{3cm}|R{3cm}|} \TBLC \end{tabular}\qquad{\huge Medium}\par 

\renewcommand{\txtcontrol}{\begin{tabular}[t]{ll}11 & 12\\ 21 & 22\\ 31 & 32\\ 41 & 42\\\end{tabular}}
\renewcommand{\txtshort}{\begin{tabular}[t]{ll}11 & 12\\ 21 & 22\\\end{tabular}}

\begin{tabular}{|T{3cm}|T{3cm}|U{3cm}|V{3cm}|} \TBLC \end{tabular}\qquad{\huge Top}\par 
\begin{tabular}{|L{3cm}|L{3cm}|C{3cm}|R{3cm}|} \TBLC \end{tabular}\qquad{\huge Medium [t]}\par 

\renewcommand{\txtcontrol}{\begin{tabular}[b]{ll} 11 & 12\\ 21 & 22\\ 31 & 32\\ 41 & 42\\\end{tabular}}
\renewcommand{\txtshort}{\begin{tabular}[b]{ll} 11 & 12\\ 21 & 22\\\end{tabular}}

\begin{tabular}{|E{3cm}|E{3cm}|F{3cm}|G{3cm}|} \TBLC\end{tabular}\qquad{\huge Bottom}\par 
\begin{tabular}{|L{3cm}|L{3cm}|C{3cm}|R{3cm}|} \TBLC \end{tabular}\qquad{\huge Medium [b]}\par 

\end{document}
1
  • 1
    (+1) for wading through such a maximal minimal ;).
    – cfr
    Commented Jun 18, 2017 at 2:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .