2

How to set a width of columns to be centered in vertical direction? Or in vertical and horizontal direction and the cells bellow as well? Thank you

\documentclass[12pt,a4paper]{report}
\usepackage{booktabs, makecell, tabularx}
\setlength\textwidth{145mm}
\setlength\textheight{247mm}
\setlength\oddsidemargin{15mm}
\setlength\evensidemargin{15mm}
\setlength\topmargin{0mm}
\setlength\headsep{0mm}
\setlength\headheight{0mm}
\let\openright=\clearpage
\begin{document}
 \begin{table}
\small
\renewcommand{\arraystretch}{1.2}
\begin{tabularx}{\textwidth}{>{\hsize=0.2\hsize}X
                             >{\hsize=0.25\hsize}X
                             >{\hsize=0.25\hsize}X
                             >{\hsize=0.3\hsize}X}

\Xhline{1.2pt}\noalign{\vskip 0.4ex}\Xhline{1.2pt}%                          
Název souboru&  Průměrná hodnota chyby& \makecell{Odchylka určení \\ průměrné chyby}&   \makecell{Počet středovaných \\ bodů}\\
\Xhline{1.2pt}\noalign{\vskip 0.4ex}
B & A&  C&  D\\  

\Xhline{1.2pt}  
\end{tabularx}
\end{table}

\end{document}

enter image description here

1
  • 1
    Do you like the tables :-)? Why do not use the geometry package to set the page?
    – Sebastiano
    Jan 2, 2019 at 20:41

2 Answers 2

8

Combining \renewcommand\tabularxcolumn[1]{m{#1}} (from Vertical centering of all columns in \tabularx environment) and \newcolumntype{C}{>{\centering\arraybackslash}X} (from Centering in tabularx and X columns) you can achieve the following:

enter image description here

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


\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
 \begin{table}
\small
\renewcommand{\arraystretch}{1.2}
\begin{tabularx}{\textwidth}{>{\hsize=0.2\hsize}C
                             >{\hsize=0.25\hsize}C
                             >{\hsize=0.25\hsize}C
                             >{\hsize=0.3\hsize}C}

    \Xhline{1.2pt}\noalign{\vskip 0.4ex}\Xhline{1.2pt}%                          
 Název souboru&  Průměrná hodnota chyby& Odchylka určení průměrné chyby&   Počet středovaných  bodů\\
\Xhline{1.2pt}\noalign{\vskip 0.4ex}
B & A&  C&  D\\

\Xhline{1.2pt}  
\end{tabularx}
\end{table}

\end{document}

Please keep in mind that I have removed the \makecell cmmands as they distorted the vertical alignment. If you need to place the linebreaks manually, you can either use \makecell[cc]{<text>} in every column header (as suggested by Paul Stanley in the comments) or replace \newcolumntype{C}{>{\centering\arraybackslash}X} by \newcolumntype{C}{>{\centering\let\newline\\\arraybackslash}X} and use \newline where you want the linebreak to appear.

9
  • I ment centering like in the solution. Thank you. And please is the third column vertical centred? I mean that third and forth columns are lower
    – Elisabeth
    Jan 2, 2019 at 20:56
  • 1
    @Elisabeth: makecell seems to have interfered with the vertical centering of the m type columns. I have therefor removed it.
    – leandriis
    Jan 2, 2019 at 21:01
  • @leandriis Now you have eliminated the \makecells, which corrects the vertical spacing, but alters the substance. Probably better to put all the headings in \makecells or add an optional vertical positioning argument to them? Jan 2, 2019 at 21:04
  • Thank you very much. It is exactly what I expected.
    – Elisabeth
    Jan 2, 2019 at 21:05
  • @PaulStanley: What do you mean by 'alter the substance'?
    – leandriis
    Jan 2, 2019 at 21:08
3

Improving a bit upon the nice solution of @leandriis, I would also remove all manual rule width manipulations and just use booktabs macros, they take care of widths and separation nicely. No need for makecell as also stated by leandriis.

If you want to break lines at certain points and used makecell to do that, then you can still use \newline for that.

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

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
 \begin{table}
\small
\renewcommand{\arraystretch}{1.2}
\begin{tabularx}{\textwidth}{>{\hsize=0.2\hsize}C
                             >{\hsize=0.25\hsize}C
                             >{\hsize=0.25\hsize}C
                             >{\hsize=0.3\hsize}C}
\toprule%                          
Název souboru & Průměrná hodnota chyby & Odchylka určení průměrné chyby & Počet středovaných bodů \\
\midrule
B             & A                      & C                              & D                       \\
\bottomrule 
\end{tabularx}
\end{table}

\end{document}

enter image description here

2
  • Did you test the part about \newline with your above example? If I add this to your code, the first line is not horizontally centered: i.stack.imgur.com/SIJx3.png You can bypass this using \newcolumntype{C}{>{\centering\let\newline\\\arraybackslash}X} instead.
    – leandriis
    Jan 2, 2019 at 21:22
  • 1
    \par can be used instead.
    – AboAmmar
    Jan 2, 2019 at 22:04

You must log in to answer this question.

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