3

While using this table environment, I need to get the cell contents of the second row to be in the middle of each cell. But, I am getting all of them aligned to top of the cells (although there are centered vertically). Can anybody guide me what mistake I am doing?

table

 \documentclass[12pt,twoside,openright]{report}
 \usepackage[linktocpage,colorlinks=true,linkcolor=black,citecolor=black]{hyperref}
 \usepackage{amsmath}
 \usepackage{amssymb}
 \usepackage{float}
 \usepackage{multirow}
 \usepackage{multicol}
 \usepackage{array,booktabs}
 \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
 \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
 \usepackage{longtable}
 %\usepackage[dvips]{color}  % comment it to run with png images and pdflatex
 %\usepackage{epsfig} %Sharma commented
 \usepackage{graphicx} % Sharma introduced

 %\usepackage{mciteplus}
 \usepackage[sectionbib]{chapterbib}
 \usepackage[sort&compress,numbers]{natbib}
 \usepackage[nottoc]{tocbibind}

 \usepackage{enumerate}
 \usepackage{bm}
 \usepackage{braket}
 \usepackage{microtype}
 \usepackage[titletoc]{appendix}
 \usepackage{color}
 \usepackage{fixltx2e}
 \usepackage[top=1.0in, bottom=1.0in, left=1.0in, right=1.0in]{geometry}

 \setlength{\textheight}{23cm}
 \setlength{\textwidth}{15cm}

 \setlength{\oddsidemargin}{0.4in}
 \setlength{\evensidemargin}{0in}

 \begin{document}

 \begin{table}
 \centering
 \resizebox{\columnwidth}{!}{%
 \begin{tabular}{|*{16}{>{\centering\arraybackslash}m{0.8cm}|}} \hline

 \multicolumn{4}{|c|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{
 BN-partial-ed-GQD}  \\ \hline

 \multicolumn{2}{|M{1.6cm}|}{Avg. edge} & \multicolumn{2}{C{1.6cm}|}{Avg. 2$^{nd}$ zigzag line} &
 \multicolumn{2}{c|}{ B-border}   & \multicolumn{2}{c|}{ N-border}  &
 \multicolumn{2}{c|}{ B-edge}     & \multicolumn{2}{c|}{ N-edge} &
 \multicolumn{2}{c|}{ B-border}   & \multicolumn{2}{c|}{ N-border}  \\  \hline

 spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
 spin-${\beta}$ &
 spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
 spin-${\beta}$ &
 spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\ \hline

 19.02 & 19.02 & 21.48 & 21.49 & 20.81 & 20.81 & 22.26 & 22.26 & 11.99 & 10.82 & 10.85 & 12.01 &
 7.21 & 7.81 & 8.38 & 7.81  \\  \hline

 \multicolumn{4}{|c|}{ Charge transfer} & - 0.67 & - 0.67 & + 0.78 & + 0.78 & + 0.58 & - 0.59 & -
 0.56 &
 + 0.6 & - 0.61 & + 0.01 & + 0.57 & - 0.002 \\ \hline

 \end{tabular}
 }
 \end{table}

 \end{document}

Kindly, help me. p.s.: there are some unnecessary packages (for this table), but, they are required for my thesis. So, I kept them as it is (if by chance, they can create any problem, please do let me know).

  • 1
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document} – henrique Aug 18 '15 at 14:40
  • spin-α spin-β spin-α spin-β ... isn't this repeated information? α and β were enough and spin can be a heading. – AboAmmar Aug 18 '15 at 14:40
  • Thanks for the comment henrique. I have edited the question to exactly reproduce the problem. Just copy the code and type pdflatex *.tex. – Sharma SRK Chaitanya Yamijala Aug 18 '15 at 17:22
2

You can change p in the definition of C to m.

\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

Full code:

 \documentclass[12pt,twoside,openright]{report}
 \usepackage[linktocpage,colorlinks=true,linkcolor=black,citecolor=black]{hyperref}
 \usepackage{amsmath}
 \usepackage{amssymb}
 \usepackage{float}
 \usepackage{multirow}
 \usepackage{multicol}
 \usepackage{array,booktabs}
 \newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
 \usepackage{longtable}
 %\usepackage[dvips]{color}  % comment it to run with png images and pdflatex
 %\usepackage{epsfig} %Sharma commented
 \usepackage{graphicx} % Sharma introduced

 %\usepackage{mciteplus}
 \usepackage[sectionbib]{chapterbib}
 \usepackage[sort&compress,numbers]{natbib}
 \usepackage[nottoc]{tocbibind}

 \usepackage{enumerate}
 \usepackage{bm}
 \usepackage{braket}
 \usepackage{microtype}
 \usepackage[titletoc]{appendix}
 \usepackage{color}
 \usepackage{fixltx2e}
 \usepackage[top=1.0in, bottom=1.0in, left=1.0in, right=1.0in]{geometry}

 \setlength{\textheight}{23cm}
 \setlength{\textwidth}{15cm}

 \setlength{\oddsidemargin}{0.4in}
 \setlength{\evensidemargin}{0in}

 \begin{document}

 \begin{table}
 \centering
 \resizebox{\columnwidth}{!}{%
 \begin{tabular}{|*{16}{>{\centering\arraybackslash}m{0.8cm}|}} \hline

 \multicolumn{4}{|c|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{
 BN-partial-ed-GQD}  \\ \hline

 \multicolumn{2}{|C{1.6cm}|}{Avg. edge} & \multicolumn{2}{C{1.6cm}|}{Avg. 2$^{nd}$ zigzag line} &
 \multicolumn{2}{c|}{ B-border}   & \multicolumn{2}{c|}{ N-border}  &
 \multicolumn{2}{c|}{ B-edge}     & \multicolumn{2}{c|}{ N-edge} &
 \multicolumn{2}{c|}{ B-border}   & \multicolumn{2}{c|}{ N-border}  \\  \hline

 spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
 spin-${\beta}$ &
 spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
 spin-${\beta}$ &
 spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\ \hline

 19.02 & 19.02 & 21.48 & 21.49 & 20.81 & 20.81 & 22.26 & 22.26 & 11.99 & 10.82 & 10.85 & 12.01 &
 7.21 & 7.81 & 8.38 & 7.81  \\  \hline

 \multicolumn{4}{|c|}{ Charge transfer} & - 0.67 & - 0.67 & + 0.78 & + 0.78 & + 0.58 & - 0.59 & -
 0.56 &
 + 0.6 & - 0.61 & + 0.01 & + 0.57 & - 0.002 \\ \hline

 \end{tabular}
 }
 \end{table}

 \end{document}
  • Its not working using the above manner. Can you suggest something else. – Sharma SRK Chaitanya Yamijala Aug 18 '15 at 16:52
  • Dear krnk, it worked perfectly. Thanks a lot. If possible, could you please explain, why the same code didn't work, when I have used newcolumtype as 'M', but working with 'C'. I couldn't see any difference except the letters. Please let me know any obvious mistake which I am doing. Thanks once again. – Sharma SRK Chaitanya Yamijala Aug 19 '15 at 4:51
3

The best is to use tabularx here, I think. Also, one can play with the values of \tabcolsepand the size of the font. With \scriptsize, the last row cells can use only one line. I added the cellspace package, to have some vertical padding at the top and bottom of cells, and replaced 2$^{nd}$ (which is wrong anyway: it produces an italic nd, like two variables) with 2\textsuperscript{nd}. Finally, I replaced your C column type (its widthcalculated width did not talke into account the value of tabcolsep and arrayrulewidth) with a c \multicolumn, and used makecell to have manual line breaks where needed. No \adjustwidth is necessary:

\documentclass[a4paper]{article}
\usepackage[hmargin=2.5cm, showframe, nomarginpar]{geometry}
\usepackage{float}
\usepackage{multirow}
\usepackage{array,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}S{m{#1}}}
\usepackage{longtable}
\usepackage{adjustbox}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{makecell}
\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}
\addparagraphcolumntypes{X, C}

\begin{document}

\begin{table}
  \centering\setlength\tabcolsep{3pt}\footnotesize
  \begin{tabularx}{\linewidth}{|*{16}{S{X}|}} \hline
    \multicolumn{4}{|Sc|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{ BN-partial-ed-GQD} \\
    \hline
    \multicolumn{2}{|Sc|}{Avg. edge} & \multicolumn{2}{Sc|}{\makecell{Avg. 2\textsuperscript{nd} \\ zigzag line}}
    & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} & \multicolumn{2}{c|}{ B-edge} & \multicolumn{2}{c|}{ N-edge} & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} \\
    \hline
    spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\
    \hline
  \end{tabularx}
\end{table}

\begin{table}[!ht]
  \centering\setlength\tabcolsep{1.5pt}\scriptsize
  \begin{tabularx}{\linewidth}{|*{16}{S{X}|}} \hline
    \multicolumn{4}{|Sc|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{ BN-partial-ed-GQD} \\
    \hline
    \multicolumn{2}{|c|}{Avg. edge} & \multicolumn{2}{Sc|}{\makecell{Avg. 2\textsuperscript{nd}\\ zigzag line}} & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} & \multicolumn{2}{c|}{ B-edge} & \multicolumn{2}{c|}{ N-edge} & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} \\ \hline
    spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\
    \hline
  \end{tabularx}
\end{table}

\end{document}

enter image description here

  • this answer also is not working :( – Sharma SRK Chaitanya Yamijala Aug 18 '15 at 17:04
  • I finally understood why: the C column type width wasl calculated not taking into accounts all the parameters (some explanations in my updated answer). It was simpler to use the c type, and use \makecell, which allows for line breaks inside cells. – Bernard Aug 18 '15 at 17:52
  • Hi Bernard. Thanks for your effort. Its also working. But, I need to adopt many new things, which I am not interested to do at this moment. Erek answer suits me the best. Thanks for your help. – Sharma SRK Chaitanya Yamijala Aug 19 '15 at 6:00
2

Is this what you looking for?

enter image description here

Edit (2): After edition of your question I adopt my answer to new data which you provide. In this I change your preamble:

  • move hypperref package to the end of preamble, where it should be (since it can change some settings in other packages)
  • move geometry package on the place immediately after documentclass
  • remove settings for text size ... because they are already determined by˛geometry package
  • comment all package which I haven't installed in my laptop and which not influence on table formatting

    In design of your table I use your definition for column type M. I use iz in tabular preamble as well in multicolumns in the second row of table. Also I remove resizebox, to my taste its use in such cases should be avoided. Beter is determined smaller font-- in this case I use \footnotesize -- and reduce inter column space.

Code:

 \documentclass[12pt,twoside,openright]{report}
 \usepackage[top=1.0in, bottom=1.0in, left=1.0in, right=1.0in]{geometry}

 \usepackage{amsmath}
 \usepackage{amssymb}
 \usepackage{float}
 \usepackage{multicol}

 \usepackage{longtable}
 \usepackage{multirow}
 \usepackage{array,booktabs}
 \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
 \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
 %\usepackage[dvips]{color}  % comment it to run with png images and pdflatex
 %\usepackage{epsfig} %Sharma commented
 \usepackage{graphicx} % Sharma introduced

 %\usepackage{mciteplus}
 %\usepackage[sectionbib]{chapterbib}
 %\usepackage[sort&compress,numbers]{natbib}
 %\usepackage[nottoc]{tocbibind}

 \usepackage{enumerate}
 \usepackage{bm}
 \usepackage{braket}
 \usepackage{microtype}
 %\usepackage[titletoc]{appendix}
 \usepackage{color}

\usepackage[linktocpage,colorlinks=true,linkcolor=black,citecolor=black]{hyperref}
\usepackage{fixltx2e}

%surplus settings ... this determine geometry package!
%\setlength{\textheight}{23cm}
%\setlength{\textwidth}{15cm}
%\setlength{\oddsidemargin}{0.4in}
%\setlength{\evensidemargin}{0in}

    \begin{document}
    \medskip
\begin{table}[h]
    \setlength{\tabcolsep}{3pt}
    \centering
    \footnotesize
    \begin{tabular}{|*{16}{>{\centering\arraybackslash}M{0.8cm}|}}          \hline
\multicolumn{4}{|c|}{ GQD}
    &   \multicolumn{4}{c|}{BN-zigzag-ed-GQD}
        &   \multicolumn{8}{c|}{BN-partial-ed-GQD}                      \\  \hline
\multicolumn{2}{|M{1.6cm}|}{Avg. edge}
    &   \multicolumn{2}{M{1.6cm}|}{Avg. 2$^{nd}$ zigzag line}
        &   \multicolumn{2}{M{1.6cm}|}{ B-border}
            &   \multicolumn{2}{M{1.6cm}|}{ N-border}
                &   \multicolumn{2}{M{1.6cm}|}{B-edge}
                    &   \multicolumn{2}{M{1.6cm}|}{ N-edge}
                        &   \multicolumn{2}{M{1.6cm}|}{ B-border}
                            &   \multicolumn{2}{M{1.6cm}|}{ N-border}   \\  \hline
spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$  & spin-${\alpha}$ & spin-${\beta}$ \\ \hline
    \end{tabular}
\end{table}
    \end{document}

The above code is tested.

IN my answer I focused only to vertically and horizontally centering of cells content. If you like to improve table layout, please consider to use makecell package and its macro \thead{...} for settings heads of table columns and other possibilities for enlarge gaps above and below cell contents (see package documentation).

  • Unedited answer is already what I have. I have tried with the edited answer as well. But, that's also not working. Have you tried? – Sharma SRK Chaitanya Yamijala Aug 18 '15 at 17:02
  • @SharmaSRKChaitanyaYamijala, my MWE is tested, By it I generate provided picture of table. And now I tested again. It works for sure. In your code in question is error, one mulltiline isn't close with }. I test I use MikTeX, recent 64-bit version. If my code doesn't work, please tell me what is the error. In my answer, as you can see, I didn't bother with width of table. I can suggest you, how I will make it wider than text (without resizebox), if you wish (this was not your question). – Zarko Aug 18 '15 at 17:31
  • Thanks for your help. Your answer is also working. I prefered Krnk's answer as it require less changes to my document. Thank you for your time. – Sharma SRK Chaitanya Yamijala Aug 19 '15 at 6:05

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.