2

Question

In a tabular, how to ensure all rows have a vertically centered content and a height greater or equal to a given value (e.g. 0.8 cm)? (See MWE at bottom of the question). (The cell's content should not shrink or get enlarged).

enter image description here

Context and details

I'm writing a software that creates LaTeX documents automatically: maths worksheets for pupils. Among them, there's the "mental calculation" family of documents, where all questions are organized in a tabular of 10 or more questions.

Three columns are visible:

  • the left one (number of the question),
  • the middle one (the question itself, that may be one or two lines of text, sometimes a geometric figure is included, that needs more space),
  • and the right one (either empty, on the first page, where pupils write their answers, or filled with the answer on the second page).

Here is how it looks like (text is in french, but you get the general idea?):

enter image description here

Problem

The content of the cells of the central column are roughly vertically centered, but this centering is obtained taking advantage of a bug in the array package that is now fixed (see this question). Hence, using a newer version of array produces vertically top aligned cells instead of vertically centered cells like in the picture above.

Nevertheless, I still would like to vertically center the content of the left and middle columns, may it be text or figures (or both).

Plus, there's a second constraint that requires to be fulfilled: I would like to ensure any row is at least 0.8 cm high (not fixed to 0.8 cm, but at least 0.8 cm) because pupils will need to write in the cells at right and the rows need to be high enough for that. Not all pupils have a tiny handwriting!

From @Bernard's answer I know the cellspace package could help to keep the vertical alignement. So I thought maybe I could set the top and bottom space values to 0.3cm, what would make 0.6cm altogether (plus the height of the cell's content, so at least 0.8cm altogether). But in cells containing geometric figures, such an extra space is not required and would add too much useless height to the cell (that would lead some tabulars to not fit into one page, only because some cells are too high).

I do not see how to use \arraystretch to ensure a minimum height of 0.8 cm (that's only a coefficient).

Is it possible to fulfill these two constraints? It's possible to start from this example (where cells are vertically top centered if you use a recent version of array):

\documentclass[a4paper, 12pt]{article}

\usepackage{amsmath}
\usepackage{array}

\begin{document}
\normalsize
\textbf{Some title}
\newline 
\newline 
\begin{tabular}{m{0.5 cm}>{}m{10 cm}|>{}m{4 cm}@{}m{0pt}@{}}
\hline 
\textbf{1.} & $11 \times \text{?} = 9$ & & \tabularnewline [0.8cm] \hline 
\textbf{2.} & $\text{?} \times \text{?} = 72$ & & \tabularnewline [0.8cm] \hline 

\end{tabular}

\end{document}

Notes:

  • I do not want to use another tabular package (like longtable, tabularx, booktab etc.).
  • I won't remove the vertical line separating the column at right because that makes a clear separation between the wording and the pupils' answers.
  • you can use \arraystretch so long as you work out what .8cm divided by the current baseline (eg 12pt) is. or just think in baselines rather than cm and specify it that way initially... – David Carlisle Sep 6 '18 at 15:57
  • I can calculate values that will be written in the final LaTeX document. I just have no clue about the relationship between a minimal height (.8 cm) and the current baseline (where is this baseline defined? I only experimentally tweaked the baseline of the TikZ pictures to align them better, what is certainly not the best way to do it...). – zezollo Sep 6 '18 at 16:00
  • 12pt article sets it to 14.5pt for \normalsize (you can always add \showthe\baselinekip and tex will show the value. – David Carlisle Sep 6 '18 at 16:06
  • You can try >{\rule[-0.4cm]{0pt}{0.8cm}}m{0pt} for the last column. – Bernard Sep 6 '18 at 17:40
  • @Bernard sorry I am really no expert, if I replace @{}m{0pt}@{} by >{\rule[-0.4cm]{0pt}{0.8cm}}m{0pt} it has no effect (maybe because of the use of the more recent array version?) – zezollo Sep 7 '18 at 13:47
1

mostly off-topic (since ideas how to solve your problem are explained in comments to question):

  • for table is used the array package
  • for inserting images (pdf, png, ... files) a macro \adjustimage{<keays>}{image file name>} from the adjustbox package is used
  • for including images as code is used macro \adjustbox{<keys>}{<image code>} also from the package adjustbox
  • added is automatic numbering of the table's rows
  • for units i suggest to use the package siunity
  • minimal vertical size of rows is determined by \rule[-2ex]{0pt}{5ex} which is inserted on the end of the first column (by this extra column is not needed anymore)
  • the width of the columns is defined by use of the \dimexpr so that the width of the table is as wide as the text

    \documentclass[a4paper, 12pt]{article}
    
    \usepackage{amsmath}
    \usepackage{array}
    \newcounter{task}
    \newcolumntype{N}{>{\refstepcounter{task}\thetask%
                         \bfseries}p{\dimexpr0.05\linewidth-1.33\tabcolsep-0.33\arrayrulewidth}%
                      <{.\rule[-2ex]{0pt}{5ex}}}
    \usepackage[export]{adjustbox}
    \usepackage{siunitx}
    \usepackage{tikz}
    
    %---------------- show page layout. don't use in a real document!
    \usepackage{showframe}
    \renewcommand\ShowFrameLinethickness{0.15pt}
    \renewcommand*\ShowFrameColor{\color{red}}
    %---------------------------------------------------------------%
    \begin{document}
    \section*{Some title}
    
    \begin{center}
    \adjustboxset{margin=0pt 3pt 0pt 6pt}
    \setlength\arrayrulewidth{0.5pt}
    \begin{tabular}{@{}
                    N  
                    m{\dimexpr0.625\linewidth-1.33\tabcolsep-0.33\arrayrulewidth} | 
                    m{\dimexpr0.325\linewidth-1.33\tabcolsep-0.33\arrayrulewidth} 
                    @{}}
        \hline
    & $11 \times \text{?} = 9$          &                           \\ \hline
    & $\text{?} \times \text{?} = 72$   & some long\newline
                                              long long\newline
                                              long text             \\ \hline
    &  \adjustimage{width=3cm}{example-image-duck}
                                        & \si{\square\deci\metre}   \\ \hline
    & some long\newline long long\newline long text
                                        & ~                         \\ \hline
    &  \adjustbox{width=2cm}{\tikz\node[draw,minimum size=12mm] {tikz image};}
                                        & \si{\cubic\metre}         \\ \hline
    \end{tabular}
    \end{center}
    \end{document}
    

enter image description here

(red lines indicate text border)

  • If I understand well, you get the vertical alignment and the minimal height when you insert an (invisible) rule in the first column: \rule[-2ex]{0pt}{5ex}? 5ex is about 0.8cm, 0pt is the width (what make the rule not visible) and [-2ex] "centers" it lower than the baseline and hence, produces the vertical alignment? I don't know how your answer is off-topic! – zezollo Sep 7 '18 at 14:29
  • @zezollo, because this idea (suggested in to use in special column of zero width, however i move rule to the first column,) was suggested in comments. :-). however, my answer address issues, which you not ask, but i suspect that those suggestion can be helpful in design of your tables/documents. – Zarko Sep 7 '18 at 14:36
  • Okay, this was @Bernard's idea! I was just not able to understand it neither to make it work, but now I get it. The idea of the adjustbox package for pictures is indeed interesting too. I'd need it for TikZ pictures. Thanks! – zezollo Sep 7 '18 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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