2

I know this question has asked many times before, but is there maybe another efficient way to really vertically centre text in a tabularx environment?

\documentclass[a4paper,9pt]{extarticle}
\usepackage[a4paper,left=1.8cm,right=1.8cm,top=2.5cm,bottom=1.4cm,footskip=0cm,headheight=0cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{xcolor}
  \definecolor{table_lines}{cmyk}{0, 0, 0, 0.24} % define color
\usepackage{fancyhdr} % page numbering
\renewcommand{\headrulewidth}{0pt} % remove top line
\usepackage{colortbl} % column lines color
\usepackage{tabularx} % different column width
  \newcolumntype{L}[1]{>{\hsize=#1\hsize\raggedright\arraybackslash}X}
  \newcolumntype{C}[1]{>{\hsize=#1\hsize\centering\arraybackslash}X}
  \makeatletter
    \newcommand{\thickhline}{\noalign {\hrule height 2pt}}
    \newcolumntype{'}{!{\color{table_lines}\vrule}}
  \makeatother
\usepackage{tcolorbox} % fancy section
\renewcommand*\footnoterule{} % remove footnote line
\usepackage{array}
\makeatletter
  \def\@seccntformat#1{\llap{\csname the#1\endcsname\quad}}
  \def\tabularxcolumn#1{m{#1}} % vertical center in tabualrx
\makeatother


\begin{document}
\renewcommand{\tabularxcolumn}[1]{>{\arraybackslash}m{#1}}
\begin{center}
  \begin{tabularx}{\textwidth}{' L{0.20} '@{}L{0.53} ' C{0.27} '}
    \arrayrulecolor{table_lines}\hline
    0 & 11/02/2011 & \raisebox{-0.05cm}{1} \\[0.0775cm]\hline
    Hallelujah & 04/17/2012 & \raisebox{-0.05cm}{1} \\[0.0775cm]\hline
    1 & 07/05/2013 & \raisebox{-0.05cm}{0} \\[0.0775cm]\arrayrulecolor{table_lines}\hline
  \end{tabularx}
\end{center}
\vspace*{0.7cm}
\end{document} 

The results is as in the picture and you can see that the text is not really centred. enter image description here

  • The entries are only one line, so vertical centre is almost indistinguishable from top or bottom alignment if you apply it to all the columns. Did you mean horizontal alignment???? – David Carlisle Sep 30 '15 at 20:19
  • yea, it may be hard to see but they are not really vertically centred, but this is what I am actually looking for. Maybe it isn't so easily doable, but that is what I want to find out. – Christovis Sep 30 '15 at 20:24
  • @Hiro They are vertically centered. See slashes. – Przemysław Scherwentke Sep 30 '15 at 20:28
  • @Hiro the vertical centres of each entry are lined up, but as the entries in each column are single row they are all aligned just as if they had baseline alignment. For such a table anything other than baseline alignment is going to look really odd anyway! – David Carlisle Sep 30 '15 at 20:33
  • 1
    @buhtz why did you ping me It wasn't my example:-) – David Carlisle Dec 11 '16 at 23:32
1

Mostly the alignment was off due to the \raisebox and extra length specified to \\[.] however you also want to add a little extra space above each row to compensate for the visual effect of the descender on / making the digits appear over-high within their row.

Something like

enter image description here

\documentclass[a4paper,9pt]{extarticle}
\usepackage[a4paper,left=1.8cm,right=1.8cm,top=2.5cm,bottom=1.4cm,footskip=0cm,headheight=0cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{xcolor}
  \definecolor{table_lines}{cmyk}{0, 0, 0, 0.24} % define color


\usepackage{colortbl} % column lines color
\usepackage{tabularx} % different column width
  \newcolumntype{L}[1]{>{\hsize=#1\hsize\raggedright\arraybackslash}X}
  \newcolumntype{C}[1]{>{\hsize=#1\hsize\centering\arraybackslash}X}
  \makeatletter
    \newcommand{\thickhline}{\noalign {\hrule height 2pt}}
    \newcolumntype{'}{!{\color{table_lines}\vrule}}
  \makeatother




\begin{document}

\begin{center}
  \renewcommand\arraystretch{1.2}
  \addtolength\extrarowheight{1.5pt}
  \begin{tabularx}{\textwidth}{' L{0.20} '@{}L{0.53} ' C{0.27} '}
    \arrayrulecolor{table_lines}\hline
    0 & 11/02/2011 & 1 \\\hline
    Hallelujah & 04/17/2012 & 1 \\\hline
    1 & 07/05/2013 & 0 \\\arrayrulecolor{table_lines}\hline
  \end{tabularx}
\end{center}
\vspace*{0.7cm}
\end{document} 

Incidentally as there is no line breaking within the rows tabularx isn't doing very much useful work here, I'd simply use tabular (although the vertical spacing is the same)

| improve this answer | |

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.