# Top-aligned table cells with different font sizes and controlled spacing

I am trying to achieve a table where a few things have to work simultaneously. Several of these things have been discussed before, but I have been unable to make everything work at once (and some things at all).

I want to be able to use different font sizes in cells on the same row, and have them precisely aligned at the top. That has almost been achieved in the MWE below, but the letters of the smaller font are slightly below the letters of the larger font. (Previous discussion: Make different font sizes in a table align at top, not bottom, but the alignment is only close, not perfect.) Edit: The top alignment should be for the maximum height of the font used, so that an 'a' ends up at the same height whether it is written by itself or followed by a tall letter, such as an 'l' (or, if the small 'b' next to the big 'B' in the example below is changed for a small 'a', the 'a' should not jump up to the ceiling.

I also want to be able to insert several rows of text with forced line breaks within a cell of the table, and those rows need to have the same spacing between them as normal text has. That has been achieved by nesting another table in a cell in the MWE below.

In addition, I want to be able to use \hfill to put something out in the right margin from within the nested table, which works in the MWE.

Further, the margins on the top, bottom and sides of the table should be zero, so that only the page margins determine the amount of white space around the table (as it is now, there is even a difference between the right and left margins -- although the margins were for some reason adjusted when I uploaded the pdf below, it is visible if you compile the code and look at the pdf directly).

Lastly, I would like to get rid of all automatic spacing between cells, and then define my own spacing. Preferably, I would like to be able to define the vertical spacing between cells in terms of line heights (for lines of the size that I specify for normal text, so 11pt in the example below), while the units of horizontal spacing would preferably be defined in terms of "the width of an 'm'", or something like that.

\documentclass[paper=a4,fontsize=11pt]{scrartcl} % KOMA-article class
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel} % English language/hyphenation
\usepackage{geometry}
\usepackage{tabu}
\usepackage{lipsum}

\newcolumntype{V}{>{\vspace{0pt}}p}

\begin{document}
\begin{tabular} [t]{@{}V{0.2\columnwidth}V{0.8\columnwidth}} % @{} is used to remove the margins from cells
\Huge\textbf A & \begin{tabular}[t]{@{}p{0.8\columnwidth}}
Lorem ipsum \hfill right-aligned\\
sem.\\
Lorem\\
ipsum
\end{tabular} \\
\Huge\textbf B & b \\
\Huge\textbf C & \lipsum*[1] \\
\end{tabular}
\end{document}


Edit 2013-03-25: I tried a different approach (with minipages) and asked a question: Make a list with uniform spacing regardless of the characters and number of text lines, possibly with minipages or a table. As of writing this, I have one answer that is similar to the accepted answer below. It achieves correct spacing, but is limited to different sizes in the left column (if one wants to use \hfill in the right column), and the top alignment for the \Huge text is not perfect.

Edit 2013-03-25: I got an answer in the thread Make a list with uniform spacing regardless of the characters and number of text lines, possibly with minipages or a table where \strut was used to add space, and \vphantom{Qp} (adds the vertical space of what is written in the braces(Qp in this example)). That could most likely be used here to, to get the correct spacing between cells even if only short letters or letters that do not protrude downwards are used.

-

A cell always start with a strut, precisely \@arstrut, which is meant to help in alignment. The \Huge letters in the first column are higher than this strut, so they are set touching the upper margin. This doesn't happen for the second column cells. A trick can be to provide a fake first line and then backing up by its height, which we know precisely to be the height of the strut. Then we insert no interline glue.

However this won't work with a nested tabular.

\documentclass[paper=a4,fontsize=11pt]{scrartcl} % KOMA-article class

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel} % English language/hyphenation
\usepackage[showframe]{geometry}
\usepackage{array,tabularx}
\usepackage{lipsum}

\makeatletter
\newcolumntype{A}[1]{>{\kern-\ht\@arstrutbox\hrule height 0pt}p{#1}}
\newcolumntype{V}{%
>{\csname par\endcsname\kern-\ht\@arstrutbox\nointerlineskip\hrule height0pt
\mbox{}\csname par\endcsname\kern-\ht\@arstrutbox\nointerlineskip
\vrule height\fontcharht\fontA width0pt\relax}X}
\makeatother

\begin{document}
\noindent\begin{tabularx}{\textwidth}{
@{}
A{3em}
V
@{}} % @{} is used to remove the margins from cells
\Huge\bfseries A
&
Lorem ipsum \hfill right-aligned\newline
sem.\newline
Lorem\newline
ipsum \\
\Huge\bfseries B & a \\
\Huge\bfseries C & \lipsum*[1] \\
\end{tabularx}
\end{document}


As far as the interrow spacing is concerned, I'm afraid that it's outside the tabular model of LaTeX.

Here's a different implementation, where the big letters are shifted down:

\documentclass[paper=a4,fontsize=11pt]{scrartcl} % KOMA-article class

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel} % English language/hyphenation
\usepackage[showframe]{geometry}
\usepackage{lipsum}

\newcommand{\bigletter}[1]{%
\raisebox{\dimexpr-\height+\fontcharht\fontA\relax}{\Huge\bfseries #1}%
}

\makeatletter
\makeatother

\begin{document}
\noindent\begin{tabularx}{\textwidth}{
@{}
p{3em}
X
@{}} % @{} is used to remove the margins from cells
\bigletter{A}
&
Lorem ipsum \hfill right-aligned\newline
sem.\newline
Lorem\newline
ipsum \\
\bigletter{B} & a \\
\bigletter{C} & \lipsum*[1] \\
\end{tabularx}
\end{document}

-
That is a nice and informative answer. You gave me an example of how to do what I asked for, and I realized that I had got one detail slightly wrong when I asked the question. I wanted the top alignment to be for the font and size used, but not for the actual character that was on the line. That would mean that the 'a' in your answer should ideally not have jumped all the way to the ceiling, but rather stayed at the same height as if followed by a tall character like an 'l'. I have edited my question to also include this. –  hjb981 Mar 24 '13 at 18:07
It is perhaps not the most elegant way, but one way of solving the different alignment of a short letter compared to a tall letter would be to add an invisible (white) tall letter after the short one, like: a\textcolor{white}{l}. The only time that would not work is if the whole row was filled with short letters, which probably would not happen very often. –  hjb981 Mar 24 '13 at 18:20
Just one more thing -- how do I adjust the spacing between table rows? –  hjb981 Mar 24 '13 at 18:21
I was able to adjust the spacing using \vspace (I had to insert it for both columns, but it worked). Both positive and negative values were possible, so the only thing I lack now is a way to access the inter-row spacing (to know how much negative \vspace to add to get the same line distance as within a paragraph, although admittedly, most of the time some extra space would be preferable). –  hjb981 Mar 24 '13 at 20:53
The second implementation nicely achieved the kind of top alignment I wanted. I have discovered another thing: the bottom part of the letters still have an effect on spacing. For example, if \bigletter{B} is changed to \bigletter{By}, the position of the row below is effected. Is there a way to circumvent that, so that the spacing between rows is always the same as if the rows contained both tall letters (like 'b', 'd' etc or any capital letter) and letters that protrudes downwards (like 'g', 'j', 'p' and so on)? –  hjb981 Mar 24 '13 at 22:08