1

I'm using tabularx with this solution: https://tex.stackexchange.com/a/12712 to achieve right alignment in a variable column width based on content size with linebreaks.

I would like the result to look like this: Table

The problem is the border between X | X columns is missing:

\documentclass{article}

\usepackage{tabularx}    %% tables with auto size
\usepackage{array}   %% tables content alignment
\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{document}

  \begin{center}
    \begin{tabularx}{\textwidth}{| X | X | R{2cm} | R{2cm} |}
      \hline
      Column1 & Column2 & Column3 & Column4 \\ \hline
      a & Some Value & This is a very long right-aligned text that should break automatically & This is a very long right-aligned text that should break automatically \\ \hline
      b & Some Value & Shorter right-aligned text & Shorter right-aligned text \\ \hline
    \end{tabularx}
  \end{center}

\end{document}
  • 1
    I ran your test file and all the vertical rules appear – David Carlisle May 26 '16 at 8:51
  • 1
    This is the 'usual PDF viewer does not show lines' issue eventually. Zoom in and the rules appear, most likely – user31729 May 26 '16 at 8:53
  • Ugly, vertical lines on my system, too, but there is no top rule due to missing \hline after the table preamble (i.e. before Column1). Use booktabsin stead and get rid of the vertical lines. – Sveinung May 26 '16 at 8:54
  • Yes! Opening the PDF file in a different PDF viewer helped. The missing \hline was a typo. But I realized the table headers are still aligned left. Is it possible to align them center? If this is presented as an answer with mentioning the PDF Viewer problem I will gladly accept it. – Joudicek Jouda May 26 '16 at 9:05
  • 1
    \multicolumn{1}{c|}{heading} (or |c| for the first column) – David Carlisle May 26 '16 at 9:34
2

to answer the question about headings asked in comments, you can over-ride the right alignment using \multicolumn{1}{c} or just simply using \centering since all the columns are p (or equivalently, X) columns so parboxes.

enter image description here

\documentclass{article}

\usepackage{tabularx}    %% tables with auto size
\usepackage{array}   %% tables content alignment
\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{document}

  \begin{center}
\setlength\extrarowheight{2pt}
    \begin{tabularx}{\textwidth}{| X | X | R{2cm} | R{2cm} |}
      \hline
\centering\bfseries
      Column1 & 
\centering\bfseries
Column2 &
\centering\bfseries
Column3 &
\centering\bfseries
Column4 \tabularnewline \hline
      a & Some Value & This is a very long right-aligned text that should break automatically & This is a very long right-aligned text that should break automatically \\ \hline
      b & Some Value & Shorter right-aligned text & Shorter right-aligned text \\ \hline
    \end{tabularx}
  \end{center}

\end{document}
| 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.