3

I have been trying numerous ways to get the vertical centering on the Latex table I'm working on. But it always gives a horizontally centered yet vertically on top alignment. what can I do to make it vertically centered?

\begin{center}
\centering
\begin{tabular}{|p{5in}|p{.5in}|} \hline
\multicolumn{2}{|c|}{\textbf{Grade 11}} \\ [2ex] \hline
Accounting & \cellcolor{Red}Failed \\ \hline
Maths & \cellcolor{Red}Failed \\ \hline
Science & \cellcolor{Red}Failed \\ \hline
\multicolumn{2}{|c|}{\textbf{Grade 12}} \\ [2ex] \hline
Accounting & \cellcolor{Red}Failed \\ \hline
\hline
\end{tabular}
\end{center}

-------------------------------------
              Grade 11


------------------------------------- 

I need this to come on the center (horizontally and vertically):

-------------------------------------

               Grade 11

-------------------------------------

migrated from stackoverflow.com Apr 8 '16 at 0:30

This question came from our site for professional and enthusiast programmers.

2

I don't see the problem when all is wrapped in the table environment and the booktabs package is used. (If you have a comprehensive Latex installation it should be on the system.) It is one of the packages for tables that is easily recommended.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{booktabs}

\begin{table*}[!tb]
\centering
{ % scope the arraystretch command below
\renewcommand{\arraystretch}{1.5}

\begin{tabular}{ @{} p{5in} p{0.5in} @{} }  \toprule    
\multicolumn{2}{c}{ \textbf{Grade 11} } \\  \midrule 
Accounting & Failed \\ 
Maths      & Failed \\                      \bottomrule 
\end{tabular} 

} % end of \arraystretch scope. 
\caption{ An example table } 
\end{table*} 

I also pull rows apart a little more by renewing \arraystretch (only within the block), and I add @{} which remove the extra space on left and right. None of this is necessary. I dropped vertical lines, which can be added as usual. I don't have the coloring as it didn't work for me. The horizontal lines given by \toprule and \midrule have different thickness, which can also be adjusted.

Note: I don't know why the figure isn't centered here. The table is centered in the document.

table_example


If you would rather just directly fix what you have, you need to add the spacing above the line manually -- the spacing below has been added manually as well (the \\[2ex]). Just add an extra line, right above the multicolumn line

\multicolumn{2}{|c|}{ } \\                                % add this
\multicolumn{2}{|c|}{\textbf{Grade 11}} \\[2ex] \hline
 % ...

However, I can readily recommend booktabs for making tables look better. You can still tweak spacing if you wish, but that is far less likely to be needed. Another package that is commonly recommended for tables is array.

  • @checkmate I don't know whether you noticed -- I added to my answer, also showing how to simply get your code working (below the figure). Let me know if there is a problem with any of it. – zdim Apr 4 '16 at 17:30
  • Hey yes both the options are useful and thank you for your help! Just a small question... I have a very long table and halfway though the table it disappear from the page since the page is not long enough and it does not jump to the other page. what can i do? – checkmate Apr 7 '16 at 9:57
  • @checkmate Glad it helped :). A standard tool for long tables -- the ones that span over pages -- is, well, the longtable package (the link is to a pdf). Try it out and ask if it gives you trouble. There should also be plenty of discussion about it around. – zdim Apr 7 '16 at 10:00

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.