1

I know this question has been answered many times but I can't get this to work. I'm a beginner, I started learning Latex last week.

I have two types of tables. I got them to be full width and horizontally centered, but I just can't get the vertical alignment to work.

My tables look like this: My tables look like this:

What I want is:

  • full page width
  • equal column width
  • horizontally centered
  • vertically centered
  • every other row colored
  • rows slightly higher than default

Now how can I get my table cells vertically centered? Alternative solutions are welcome.

This is my code:

\documentclass[11pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[table,xcdraw]{xcolor}
\usepackage[margin=2.5cm]{geometry}

%Tabelle breite gleich & zentriert
\newcolumntype{C}{>{\centering\arraybackslash}X} 

\begin{document}  

\begin{table}[h]
\rowcolors  {2}{lightgray}{white}
\resizebox{\textwidth}{!}{%
\begin{tabularx}{1\textwidth}{l|C|C}
\rowcolor{white}
Aufgabe & Fall 1 & Fall 2     \\[1ex] \cline{1-3}
Anlage von $K_0$ & \multicolumn{2}{c}{5943,79}  \\ [1ex]
Erwirtschafteter Ertrag & 5999,40 & 10.462,50 \\ [1ex]
Anlage von Erträgen & 7.288,48 & 12.710,56 \\ [1ex]
\end{tabularx}%
}
\end{table}

\begin{table}[h]
\rowcolors  {2}{lightgray}{white}
\resizebox{\textwidth}{!}{%
\begin{tabularx}{1\textwidth}{C|C|C}
\rowcolor{white}
x / y & z / a & b / c   \\[1ex] \cline{1-3}
100 & 200 & 300  \\ [1ex]
312 & 469 & 623 \\ [1ex]
432 & 862 & 527 \\ [1ex]
\end{tabularx}%
}
\end{table}

\end{document}
3
  • welcome to tex.se! please provide complete small document (copy in your editor and past here) which start with \documentclass{...} and end{document}, which demonstrate your problem. Your link can be dead after while and people here will not be able to know, what was your problem.
    – Zarko
    Commented May 27, 2019 at 16:23
  • The first step is to learn how to paste the code here. I at least won't follow your link for such a question. Commented May 27, 2019 at 16:24
  • Really sorry! I figured it out now and changed it. Commented May 27, 2019 at 16:49

1 Answer 1

0

Like this?

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[table,xcdraw]{xcolor}
\usepackage[margin=2.5cm]{geometry}

%Tabelle breite gleich & zentriert
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\begin{document}

\begin{table}[h]
\renewcommand\arraystretch{1.3}     % <---
\rowcolors  {2}{lightgray}{white}
\begin{tabularx}{\textwidth}{L|C|C} % <---
\rowcolor{white}
Aufgabe                 & Fall 1     & Fall 2           \\
    \hline
Anlage von $K_0$        & \multicolumn{2}{c}{5943,79}   \\
Erwirtschafteter Ertrag & 5999,40   & 10.462,50         \\
Anlage von Erträgen     & 7.288,48  & 12.710,56         \\
\end{tabularx}%
\end{table}

\begin{table}[h]
\renewcommand\arraystretch{1.3}     % <---
\rowcolors  {2}{lightgray}{white}
\begin{tabularx}{\textwidth}{C|C|C} % <---
\rowcolor{white}
x / y & z / a & b / c   \\
    \cline{1-3}
100 & 200 & 300         \\
312 & 469 & 623         \\
432 & 862 & 527         \\
\end{tabularx}%
\end{table}

\end{document}

Addendum: An alternativer solution is use the cellspace packet and larger spaces above and below of cells' contents:

\documentclass[11pt,a4paper]{article}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\usepackage{cellspace, 
            tabularx}
\setlength\cellspacetoplimit{2ex}
\setlength\cellspacebottomlimit{2ex}
\usepackage[margin=2.5cm]{geometry}

%Tabelle breite gleich & zentriert
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\begin{document}

\begin{table}[h]
\rowcolors  {2}{lightgray}{white}
\begin{tabularx}{\textwidth}{SL | SC| SC} % <---
\rowcolor{white}
Aufgabe                 & Fall 1     & Fall 2           \\
    \hline
Anlage von $K_0$        & \multicolumn{2}{c}{5943,79}   \\
Erwirtschafteter Ertrag & 5999,40   & 10.462,50         \\
Anlage von Erträgen     & 7.288,48  & 12.710,56         \\
\end{tabularx}%
\end{table}

\begin{table}[h]
\rowcolors  {2}{lightgray}{white}
\begin{tabularx}{\textwidth}{SC| SC| SC} % <---
\rowcolor{white}
x / y & z / a & b / c   \\
    \cline{1-3}
100 & 200 & 300         \\
312 & 469 & 623         \\
432 & 862 & 527         \\
\end{tabularx}%
\end{table}

\end{document}

enter image description here

8
  • Sorcery! This seems to be a workaround since you can't use it with really big row heights (e.g. \renewcommand\arraystretch{5}) but for my purposes, this works. Thank you! Commented May 27, 2019 at 17:19
  • I select \extrarowheight according to your use of \\ [1ex]. if you have some other demand, you should note this in question. :-). After your comment, your question is not clear anymore. If you for example have cells with multi line context, than proposed solution is the best.
    – Zarko
    Commented May 27, 2019 at 17:25
  • Yes, the rows should be bigger, not default height. That's why I used [1ex]. But your solution works! Commented May 27, 2019 at 17:33
  • @latexcanbedifficult, see addendum to my answer. If answer solve your problem, you can accept it (by clicking on the check mark at the top left side of answer) :-)
    – Zarko
    Commented May 27, 2019 at 17:35
  • That's an even better solution, wow! Thank you, it looks great! I have another question, if you don't mind. Is there a way to vertically align ONLY the table head but nothing else? I have some tables that use the default height but the table head is a bit bigger, so I would like to center those. Commented May 27, 2019 at 18:01

You must log in to answer this question.

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