# Centering horizontally and vertically (simultaneously) using tabularx for multiline and width

Edit: (Partial solution given by This Question which was linked in comments and I've changed the code slightly; see EDIT at the bottom for the specific issue that remains)

I am trying to reproduce a specific layout of a csv file for a colleague. I've done so, but I had to do some really awful hacky things to get the spacing right on a table. The problem is that I need (more or less) specific width cells, which will force some of them to be multiple (vertical) lines, as well as needing some headers that will span multiple columns. Thus the content of the ensuing cells should ideally be centered within the (non merged) column/row intersections... but I can't seem to get it to work right without using some truly hideous \hspace* commands to force spacing eventually. Below is my MWE which is more or less the output I would like (the horizontal spacing still isn't quite right on the numbers, but I can't make it much better and it's honestly close enough) but there has to be a nice way to do this with \centering and/or the \tabularxcolumn and/or the \newcolumntype commands. Unfortunately after several hours of messing with it and reading documentation, I can't seem to figure out how to get it to work; I seem to not understand something about the syntax of these commands since all I get is errors whenever I change anything. (Side note: originally I used T for text column and wanted it to be twice the width of the S column, which would contain the numbers. I since abandoned that attempt since I can't figure out the syntax, but the commands remain).

So, to reiterate, the output is actually more-or-less fine (any improvement on the centering is great, but not really my question), but the way I get it in the code is truly hideous and I know there has to be a nice way to get this done. Google and tabularx documentation fails me (or, more accurately, I failed it) so I have to be missing some obvious bit of how the syntax is suppose to work because all I get is errors if I try to mess with it at all.

MWE:

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{tabularx}
\usepackage{amssymb}

\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{T}{X}
\newcolumntype{S}{>{\centering\arraybackslash\hsize=.5\hsize}X}%

\begin{document}
\begin{center}
\Large \textbf{Teaching Assistant Classroom Observation Sheet}
\end{center}

\begin{tabularx}{\textwidth}{XXX}
Teaching Assistant: &&Course: \\
Evaluator: &&Date:
\end{tabularx}

\section{Preparation}
\begin{tabularx}{\textwidth}{| T | c | S S |S| S S |}\hline
Question & Not Observed & \multicolumn{2}{c |}{More Emphasis Needed} & &\multicolumn{2}{c |}{Fully Accomplished}\\ \hline
TA is well-prepared and able to work homework problems&& \hspace*{0.50cm}1\hspace*{0.50cm}& \hspace*{0.30cm}2\hspace*{0.30cm}& \hspace*{0.30cm}3\hspace*{0.30cm}& \hspace*{0.50cm}4\hspace*{0.50cm}& \hspace*{0.30cm}5\hspace*{0.30cm}\\ \hline
Knows material in text and lectures&& 1& 2& 3& 4& 5\\ \hline
Problem solving techniques are consistent with lecturer and text&& 1& 2& 3& 4& 5\\\hline
Promptly returns graded assignments&& 1& 2& 3& 4& 5\\ \hline
\end{tabularx}
\end{document}


PS: I tried to search for similar topics, but only seem to find requests to do either vertical or horizontal centering, and the couple I found that tried for both didn't handle the multi-line text blocks correctly... and since I couldn't decipher the syntax I couldn't seem to fix the examples I found in those threads to work for me.

EDIT I have edited the newcolumntype and \tabularxcolumn lines in my MWE to the following:

\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{T}{X}
\newcolumntype{S}{>{\centering\arraybackslash\hsize=.5\hsize}X}%


Now I have the issue that the \multicolumn command doesn't play nicely with the X column type, leaving some awkward overhang on the \hline command. I'd like a way to make the right hand side look a bit nicer.

• in what your question differ from tex.stackexchange.com/questions/316923/…. it seems to be duplicate to it – Zarko Sep 27 '18 at 16:55
• Well, my Google-Fu has failed as I didn't find that one. It is almost perfect! The issue I have (which I had with other 'solutions' I found) is that the X column doesn't play nicely with the \multicolumn command, which I need for a couple of the headers. It makes the lines not match up quite right on the right hand side... can't seem to find a particularly good solution aside from injecting some hspace to force the line over a bit. – Jason Sep 27 '18 at 17:10

your question is actually duplicate to this question, however since you have other issues in your table code, i will propose some improvements (see code, main changes are indicated by % <---):

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{cellspace, tabularx}      % <---
\setlength\cellspacetoplimit{4pt}     % <---
\setlength\cellspacebottomlimit{4pt}  % <---

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcx[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep\centering}X|}{#1}} % <---

\begin{document}
\begin{table}[!ht]%
\setlength\tabcolsep{4pt}
\begin{tabularx}{\linewidth}{|>{\hsize=3\hsize}S{C} | c | CC | C | CC |}
\hline
Question
&   Not Observed
&   \mcx{More Emphasis Needed}
&   &   \mcx{Fully Accomplished}         \\
\hline
TA is well-prepared and able to work homework problems
&   & 1 & 2 & 3 & 4 & 5                                             \\
\hline
Knows material in text and lectures
&   & 1 & 2 & 3 & 4 & 5                                             \\
\hline
Problem solving techniques are consistent with lecturer and text
&   & 1 & 2 & 3 & 4 & 5                                             \\
\hline
&   & 1 & 2 & 3 & 4 & 5                                             \\
\hline
\end{tabularx}
\end{table}
\end{document}


which gives

• the cellspace package is used for adding some vertical space to cells' contents. this is dan with adding S specifier to selected column (in your case the firs one is appropriate)
• to preserve C cell features in preamble is added command \addparagraphcolumntypes{C}
• width of multicolumn cells is used macro \dimexpr. in calculation is consider width of two eidth of C cells and two \tabcolsep. with this its width is the same as width of two standard X, i.e. C cells.
• the table in put in float table environment that it is centered in text and that the settings inside table are limited only to this table. instead table you also can use center environment:

\begin{center}
\setlength\tabcolsep{4pt}
\begin{tabularx} ....

\end{tabularx}
\end{center}

• Ah, this is beautiful, thanks! Do you mind explaining the cellspace option and commands, and why you enclosed the tabular environment inside a table environment? I thought the point of using tabular (among others) is that it essentially already is a table environment? – Jason Sep 27 '18 at 18:01
• @Jason, see short explanation added to answer. – Zarko Sep 27 '18 at 18:16