2

The following code

\documentclass[12pt,a4paper]{article}
\usepackage{array}
\usepackage{calc}
\begin{document}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} r L{\textwidth * \real{0.8}}} 
\hline
Languages & Italian (native tongue), English (very fluent), German (beginner). \\
\hline
Chats & bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla  \\
\hline
Programming & C, Java, OCaml, Bash, Fortran. Currently learning Python and R. \\
\hline
\end{tabular*}
\end{document}

produces this:

enter image description here

If I add

\renewcommand{\arraystretch}{4}

before the tabular code, the output is this:

enter image description here

If, additionally, I change the definition of L using the p column as follows

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

then I get this:

enter image description here

None of these is the output I want. I would like every cell to be vertically centered. Why does this happen? How can I fix it? I tried to use different solutions (such as \setlength{\extrarowheight}{4pt}), but all of them are affected by the same problem.

7
  • Welcome to TeX.SE. Please tell us (a) which document class you use and (b) how the \real macro is defined.
    – Mico
    Commented Nov 8, 2016 at 13:37
  • m column type loses all information about the internal baselines as it is \vcenter if you want text in a parbox to align with a r column use p (which is vtop) Commented Nov 8, 2016 at 13:38
  • a4paper is paper size, not a document class ... it seems, that you use Awesome-CV. Please, transform your code sniped to complete small document starting with \documentclass[a4paper]{...} end ending with \end{document}.
    – Zarko
    Commented Nov 8, 2016 at 14:11
  • First of all, I'm sorry if the question is not self-contained. I had already tried to do what @Zarko suggests, but it was too complex for me to extract the relevant code and make it compilable. I am not new to LaTeX, but as I said I'm using the Awesome-CV template, which uses several LaTeX functions that are beyond my knowledge. I hoped that the code I gave was sufficient, but if it's not I can try again to make it compilable.
    – aerdna91
    Commented Nov 8, 2016 at 14:22
  • your example doesn't need the cv class you could just use article and array package to make a single tabular Commented Nov 8, 2016 at 14:24

3 Answers 3

1

You're using the wrong tool:

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx,booktabs}

\setlength{\heavyrulewidth}{\lightrulewidth}
\setlength{\aboverulesep}{12pt}% choose
\setlength{\belowrulesep}{12pt}% choose

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{} r X @{}}
\toprule
Languages & Italian (native tongue), English (very fluent), German (beginner). \\
\midrule
Chats & bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla  \\
\midrule
Programming & C, Java, OCaml, Bash, Fortran. Currently learning Python and R. \\
\bottomrule
\end{tabularx}

\end{document}

enter image description here

If you want the label vertically centered with respect to the body (but I wouldn't do it), you can set the type of the X column to m:

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx,booktabs,array}

\setlength{\heavyrulewidth}{\lightrulewidth}
\setlength{\aboverulesep}{12pt}% choose
\setlength{\belowrulesep}{12pt}% choose
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{} r X @{}}
\toprule
Languages & Italian (native tongue), English (very fluent), German (beginner). \\
\midrule
Chats & bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla  \\
\midrule
Programming & C, Java, OCaml, Bash, Fortran. Currently learning Python and R. \\
\bottomrule
\end{tabularx}

\end{document}

enter image description here

4
  • Thank you! Unfortunately I would like to have every cell vertically centered, but in your solution "Languages", "Chats" and "Programming" seem to be aligned to the first line of the cell on the right! Is there an easy fix?
    – aerdna91
    Commented Nov 8, 2016 at 16:43
  • @aerdna91 Yes, added.
    – egreg
    Commented Nov 8, 2016 at 16:46
  • Perfetct, thanks. Why do you say that "you wouldn't do it"? Also, can I ask you to suggest a good manual for tables?
    – aerdna91
    Commented Nov 8, 2016 at 16:50
  • 1
    @aerdna91 The manual of booktabs is very good for understanding how to build nice tables. I think that the picture explains by itself why I prefer the first way to the centering.
    – egreg
    Commented Nov 8, 2016 at 16:53
1

enter image description here

Issues with m column types is explained in David Carlisle answer. Unfortunately use of \arraystreatch and column type p doesn't gives equal vertical space between hlines and cells contents. Solution seems to be not use \arraystreatch and desired vertical space obtain on different way, for example with rules provided by package booktabs:

Eddit: For vertical centering of a text in the first column, you should add

\renewcommand\tabularxcolumn[1]{m{#1}}

with detewrminatin veritical space as used egreg in his answer the MWE is:

\documentclass{article}
\usepackage{booktabs,tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{L}{>{\raggedright\let\newline\\\arraybackslash}X}
\setlength{\aboverulesep}{2ex}% choose
\setlength{\belowrulesep}{2ex}% choose

\usepackage[active,floats,tightpage]{preview}
\setlength\PreviewBorder{2ex}

\begin{document}
\begin{table}[htb]% <-- added for preview
\begin{tabularx}{\textwidth}{rX}
    \midrule
Languages & Italian (native tongue), English (very fluent), German (beginner). \\
    \midrule
Chats & bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla  \\
    \midrule
\end{tabularx}
\end{table}
\end{document}

In above MWE instead tabular the tabularx environment is used. It gives equal column width as your (wrongly) used determination of the space between columns.

1
  • Thank you! Unfortunately I would like to have every cell vertically centered, but in your solution "Languages" and "Chats" seem to be aligned to the first line of the cell on the right! Is there an easy fix?
    – aerdna91
    Commented Nov 8, 2016 at 16:41
0

an m column type places the reference point in the vertical centre of its content without considering the internal baselines. so any alignment of text in an m column to text in surrounding columns is essentially accidental. (If you narrow the column slightly so that the text breaks over two lines, then it will re-align in this case)

p or b columns do set the reference point to an internal baseline (top or bottom line, respectively) and with p you get alignment again as shown below.

enter image description here

\documentclass[a4paper]{article}
\addtolength\textwidth{2cm}
\usepackage{array}
\renewcommand{\arraystretch}{4}
\begin{document}

\newcolumntype{L}[1]{%
>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\centering
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} r L{0.8\textwidth}}
\hline
Languages & Italian (native tongue), English (very fluent), German (beginner). \\
\hline
Programming & C, Java, OCaml, Bash, Fortran. Currently learning Python and R. \\
\end{tabular*}

\newcolumntype{L}[1]{%
>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\centering
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} r L{0.8\textwidth}}
\hline
Languages & Italian (native tongue), English (very fluent), German (beginner). \\
\hline
Programming & C, Java, OCaml, Bash, Fortran. Currently learning Python and R. \\
\end{tabular*}

\end{document}
1
  • Thanks for your help. I've edited the question to make it clearer and self-contained, so your answer seems odd now. I'm sorry! Anyway, the solution you suggest still doesn't solve my problem (see the new question).
    – aerdna91
    Commented Nov 8, 2016 at 14:52

You must log in to answer this question.

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