Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I center a column of a table which is raggedleft/raggedright at a specified position?

Basically the length between W and X, X and Y and Y and Z should be the same, W should be raggedright, Z should be raggedleft. The positions of W and Z are already very near to what I want but the columns under them aren't centered...

The first image is what I got already and the second one is my aim. enter image description here

enter image description here

And a MWE:

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\IfFileExists{MyriadPro.sty}{\usepackage[lf]{MyriadPro}\renewcommand{\familydefault}{\sfdefault}}{\relax}
\usepackage{array}

\begin{document}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{tabular}{m{1cm}C{1.3cm}C{1.3cm}R{1cm}}
\textbf{W}& \textbf{X} & \textbf{Y} & \textbf{Z} \\ 
\hline 
\textbf{A n.c.} & 23,2 & 23,2 & 33,4 \\
\textbf{B} & 4 & 5 & 6 \\ 
\textbf{C} & 7 & 8 & 9 \\ 
\textbf{D} & 10 & 11 & 12 \\ 
\textbf{E} & 13 & 14 & 15 \\ 
\end{tabular}

\end{document}

Most of the code I got from here.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

I'm more going towards what you're after than what you currently have...

Specifying a fixed distance between columns is best done using the @{..} specification. So, for example, one could use l@{\hspace{1cm}}c to denote a 1cm gap between a left and centred column.

enter image description here

\documentclass{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs

\begin{document}

\begin{tabular}{l@{\hspace{1cm}}c@{\hspace{1cm}}c@{\hspace{1cm}}c}
  \toprule
  \textbf{W} & \textbf{X} & \textbf{Y} & \textbf{Z} \\
  \midrule
  \rlap{\textbf{A n.c.}}\hphantom{23,2} & 23,2 & 23,2 & 33,4 \\
  \textbf{B} & 4 & 5 & 6 \\
  \textbf{C} & 7 & 8 & 9 \\
  \textbf{D} & 10 & 11 & 12 \\
  \textbf{E} & 13 & 14 & 15 \\
  \bottomrule
\end{tabular}

\end{document}

The additional play with lengths and text in the tabular body is to obtain make the columns the same widths in terms of the contents.

share|improve this answer
    
Thank you, that worked. I didn't used the additional lines and phantoms. Should I change the title to a more fitting one? And if so what would you suggest? –  Tim S. Jul 23 at 17:17
2  
@TimS.: If you'd like. How about "How to obtain a fixed gap between columns in a table?" –  Werner Jul 23 at 17:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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