4

MWE:

\documentclass{article}

\begin{document}

    \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
        \hline
        &\multicolumn{2}{|c|}{1} & \multicolumn{2}{|c|}{2} & \multicolumn{2}{|c|}{3} & \multicolumn{2}{|c|}{4} & \multicolumn{2}{|c|}{5} & \multicolumn{2}{|c|}{6} & \multicolumn{2}{|c|}{7} & \multicolumn{2}{|c|}{8}\\
        \hline
        \(f(x)\)&a&\multicolumn{2}{|c|}{b}&\multicolumn{2}{|c|}{c}&\multicolumn{2}{|c|}{d}&\multicolumn{2}{|c|}{e}&\multicolumn{2}{|c|}{f}&\multicolumn{2}{|c|}{g}&\multicolumn{2}{|c|}{h}&i\\
        \hline
        a&b&c&d&e&f&g&h&j&i&k&l&m&n&o&p&q\\
        \hline
    \end{tabular}

\end{document}

Note that the cell borders are perfectly centered:

Screenshot of the MWE above

Unfortunately, inserting (longer) formulas will destroy the layout:

% excerpt
&\multicolumn{2}{|c|}{\(-\frac{5}{4}\pi - \frac{1}{2}\)} & \multicolumn{2}{|c|}{\(-\frac{5}{4}\pi - \frac{1}{2}\)} &

Screenshot of the MWE above with longer formulas added

Questions:

1.) Is there any better way to achieve my tabular layout?

2.) If not, how can I resolve the layout problem?

(3.) Removing the last row (a – q) also causes an incorrect layout. If this behavior can be corrected in any (easy) way (maybe as a side effect of choosing another way), I'd be glad to hear about it. Otherwise, I will probably ask another question.)


I've drawn a quick sketch in Word, maybe it helps to understand my problem: Sketch

It's not perfect, but you should get the very idea.

2
  • Is this 'destroyed' layout not the result of the second row, where you use as 2nd column 'a', then a two - column wide 'b' etc, and finally a column with 'i'? Is there any particular need for this layout?
    – user31729
    Mar 11, 2014 at 18:31
  • @ChristianH. Those widths are by design. I want the cells' borders in the second row to exactly match the middles of the cells above them.
    – ComFreek
    Mar 11, 2014 at 18:39

1 Answer 1

1

You could use the tabularx package and its eponymous environment, along with a centered version of the X column type that's provided by the tabularx package, to guarantee that the basic 17 columns are all equally wide, regardless of the actual contents.

In the code below, note that I've replaced most {|c|} alignment directives with {c|} to avoid getting double-struck vertical lines.

enter image description here

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\setlength\tabcolsep{.5pt} % default value: 6pt
\begin{document}

\begin{tabularx}{\textwidth}{|*{17}{C|}} % 17 columns, all equally wide by design
\hline
& \multicolumn{2}{c|}{$-\frac{5}{4}\pi - \frac{1}{2}$} 
& \multicolumn{2}{c|}{$-\frac{5}{4}\pi - \frac{1}{2}$} 
& \multicolumn{2}{c|}{3} & \multicolumn{2}{c|}{4} 
& \multicolumn{2}{c|}{5} & \multicolumn{2}{c|}{6} 
& \multicolumn{2}{c|}{7} & \multicolumn{2}{c|}{8}\\
\hline
$f(x)$
&a
&\multicolumn{2}{c|}{b} &\multicolumn{2}{c|}{c}
&\multicolumn{2}{c|}{d} &\multicolumn{2}{c|}{e}
&\multicolumn{2}{c|}{f} &\multicolumn{2}{c|}{g}
&\multicolumn{2}{c|}{h} &i \\
\hline
a&b&c&d&e&f&g&h&j&i&k&l&m&n&o&p&q\\
\hline
\end{tabularx}
\end{document}
2
  • Awesome! Thank you very much! However, is there a way to remove the last row (a..q) without causing an incorrect reflow of the table? I noticed that \onslide from Beamer can perfectly hide that row (leaving an empty space of course - it doesn't matter). So using the internal mechanism of \onslide would be a viable option. I'am already digging in the Beamer's source code - more or less :)
    – ComFreek
    Mar 11, 2014 at 21:03
  • @ComFreek - I guess you could define a "null" cell command such as \newcommand\nn{\multicolumn{1}{C}{}} (note the uppercase C) and then replace each of the a, b, etc in the final row with \nn. Oh, that, and leave off the final \hline...
    – Mico
    Mar 11, 2014 at 21:44

You must log in to answer this question.

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