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

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
Elisabeth
  • 1,009

2 Answers2

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.

leandriis
  • 62,593
  • 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 02 '19 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 02 '19 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? – Paul Stanley Jan 02 '19 at 21:04
  • Thank you very much. It is exactly what I expected. – Elisabeth Jan 02 '19 at 21:05
  • @PaulStanley: What do you mean by 'alter the substance'? – leandriis Jan 02 '19 at 21:08
  • @leandriis Within the \makecell the OP was choosing where the linebreak went. If you take the \makecell out, that facility is lost ... which might or might make a difference: assuming the original \makecell was there for a reason (and since I don't read the language concerned I can't tell) removing it may have been a significant change. But it looks as if in this case it wasn't, since OP seems happy. – Paul Stanley Jan 02 '19 at 21:13
  • @PaulStanley: You are completely right, but since the linebreaks of the third and fourth column are identical comparing the image in the question and the output of my answer (also true if one includes the width manipulations) there is clearly ne need for makecell at least in this particular table. – leandriis Jan 02 '19 at 21:17
  • @leandriis Yes. Quite. But don't forget that a MWE may not reflect the actual final use, so maybe safer not to assume that the \makecell was not needed. At any rate your answer should point out that it is removed, because it is this that solves the vertical spacing problem. – Paul Stanley Jan 02 '19 at 21:22
  • @PaulStanley: I added a paragraph on the removal of the makecell command and two suggestions on how to achieve manual linebreaks. – leandriis Jan 02 '19 at 21:28
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

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
  • 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: https://i.stack.imgur.com/SIJx3.png You can bypass this using \newcolumntype{C}{>{\centering\let\newline\\\arraybackslash}X} instead. – leandriis Jan 02 '19 at 21:22
  • 1
    \par can be used instead. – AboAmmar Jan 02 '19 at 22:04