4

I have a multirow environment, but it does not center aligned in center environment. What might be wrong?

enter image description here

\documentclass[12pt]{article}

\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

    \begin{center}
    \begin{table}[ht]
    \caption{Virtual Machine Byte Code}\label{tab:vm}
    \begin{tabular}{lll}
    \toprule
         {\bf Group} & {\bf Name} & {\bf Parameters} \\
         \midrule\midrule[.1em]

     \multirow{4}{2.5cm}{Stack} 
        & {\tt push}
        & {\tt store} \\
     \cmidrule(lr){2-3}
        & {\tt pop}
        & {\tt success} \\
     \cmidrule(lr){2-3}
        & {\tt load}
        & {\tt success} \\    
     \cmidrule(lr){2-3}
        & {\tt save}
        & {\tt success} \\   
    \midrule[.1em] 

     \multirow{4}{2.5cm}{Stack} 
        & {\tt push}
        & {\tt store} \\
     \cmidrule(lr){2-3}
        & {\tt pop}
        & {\tt success} \\
     \cmidrule(lr){2-3}
        & {\tt load}
        & {\tt success} \\    
     \cmidrule(lr){2-3}
        & {\tt save}
        & {\tt success} \\   

    \bottomrule
    \end{tabular}
    \end{table}
    \end{center}
\end{document}
  • Change \multirow{4}{2.5cm}{Stack} to \multirow{5}{2.5cm}{Stack}. (The three \cmidrule instructions add extra (vertical) whitespace, amounting to about 1 extra "real" row.) By the way, using \midrule\midrule[.1em] seems overkill. A single \midrule should do just fine. – Mico Jun 22 '16 at 21:15
  • 3
    Never put table into \begin{center} ... \end{center}. Instead this rather write \begin{table[htb]\centering ... \end{table} – Zarko Jun 22 '16 at 21:15
  • 1
    The simplest solution is, of course, not using \multirow and letting “Stack” stay where it belong, that is, at the top of the otherwise empty space. Besides, you should know that \bf and \tt have been deprecated more than twenty years ago. – egreg Jun 22 '16 at 22:09
6

Some suggestions:

  • Change \multirow{4}{2.5cm}{Stack} to \multirow{5}{2.5cm}{Stack}

  • Replace all \cmidrule directives with \addlinespace

  • Use a single \midrule after the header row

  • Use a common line width (say, 0.1em) for all \toprule, \midrule, and \bottomrule statements.

  • Provide more structure in the setup of the tabular environment, so that you don't have to type all those \tt directives in the body of the table.

  • Don't encase the table environment in a center environment. Instead, use a single \centering statement after \begin{table}.

  • Optional: Don't use bold for the words in the header row: They already stand out sufficiently by virtue of their placement at the top of the table. :-)

enter image description here

\documentclass[12pt]{article}
\usepackage{booktabs,multirow,array,caption}

\begin{document}
    \begin{table}[ht]
    \centering
    \caption{Virtual Machine Byte Code}\label{tab:vm}
    \begin{tabular}{@{} p{2.5cm} *{2}{>{\ttfamily}l} @{}}
    \toprule[.1em]
    Group & \normalfont Name & \normalfont Parameters \\
    \midrule[.1em]
      \multirow{5}{*}{Stack} & push & store \\
      \addlinespace 
      & pop & success \\
      \addlinespace 
      & load& success \\    
      \addlinespace 
      & save& success \\   
    \midrule[.1em] 
      \multirow{5}{*}{Stack} 
      & push & store \\
      \addlinespace
      & pop  & success \\
      \addlinespace
      & load & success \\    
      \addlinespace
      & save & success \\   
    \bottomrule[.1em]
    \end{tabular}
    \end{table}
\end{document}
| improve this answer | |
  • How about if you added another line, say under the first load-line. Using \multirow{6}{*}{Stack} looks definitely wrong, same width \multirow{5}{*}{Stack}. I think you would need to use the fixup-parameter here. – Runar Jun 22 '16 at 22:23
  • @runartrollet - Honestly, my preferred approach would be to get rid of the extra spacing between the short rows. That way -- purely as a (happy) side-effect --\multirow could be used without further ado. Even better (as egreg has hinted as well, see above), don't use \multirow to begin with. :-) – Mico Jun 22 '16 at 22:57
2

For vertical positioning obey Mico comment, but you use in table commands from TeX not LaTeX, better and simpler code is:

\documentclass[12pt]{article}

\usepackage{booktabs,makecell,multirow}
\renewcommand\theadfont{\normalfont\bfseries}

\begin{document}

    \begin{table}[ht]
\centering
    \caption{Virtual Machine Byte Code}\label{tab:vm}
\begin{tabular}{p{25mm} >{\ttfamily}l >{\ttfamily}l}
    \toprule
\thead{Group} & \thead{Name} & \thead{Parameters} \\
         \midrule\midrule[.1em]

     \multirow{5}*{Stack}
        & push
        & store \\
     \cmidrule(lr){2-3}
        & pop
        & success \\
     \cmidrule(lr){2-3}
        & load
        & success \\
     \cmidrule(lr){2-3}
        & save
        & success \\
    \midrule[.1em]

     \multirow{5}*{Stack}
        & push
        & store \\
     \cmidrule(lr){2-3}
        & pop
        & success \\
     \cmidrule(lr){2-3}
        & load
        & success \\
     \cmidrule(lr){2-3}
        & save
        & success \\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

As you can see, I add package makecell for formating column heads with macro \thead{...}. Result is:

enter image description here

| improve this answer | |
1

You can move the \multirow identifiers into position using a precise \raisebox:

enter image description here

\documentclass{article}

\usepackage{booktabs,array}

\begin{document}

\begin{tabular}{ l >{\ttfamily}l >{\ttfamily}l }
  \toprule
  \bfseries Group & \rmfamily\bfseries Name & \rmfamily\bfseries Parameters \\
  \midrule\midrule[.1em]
    & push
    & store \\
  \cmidrule(lr){2-3}
  \raisebox{\dimexpr-.5\normalbaselineskip-.5\cmidrulewidth-.5\aboverulesep}[0pt][0pt]{Stack}
    & pop
    & success \\
  \cmidrule(lr){2-3}
    & load
    & success \\
  \cmidrule(lr){2-3}
    & save
    & success \\
  \midrule[.1em]
    & push
    & store \\
  \cmidrule(lr){2-3}
  \raisebox{\dimexpr-.5\normalbaselineskip-.5\cmidrulewidth-.5\aboverulesep}[0pt][0pt]{Stack}
    & pop
    & success \\
  \cmidrule(lr){2-3}
    & load
    & success \\
  \cmidrule(lr){2-3}
    & save
    & success \\
  \bottomrule
\end{tabular}

\end{document}

I've also simplified your input using \bfseries and some array insertions via >{\ttfamily} before each of the last two columns.

| improve this answer | |
1

From the multirow-documentation:

It’s just about impossible to deal correctly with descenders. The text will be set up centred, but it may then have a baseline that doesn’t match the baseline of the stuff beside it, in particular if the stuff beside it has descenders and text does not. This may result in a small misalignment. About all that can be done is to do a final touchup on text, using the fixup optional argument. (Hint: If you use a measure like .1ex, there’s a reasonable chance that the fixup will still be correct if you change the point size.)

So we need to use the fixup-parameter.

Output

enter image description here

Code

\documentclass[12pt]{article}

\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

    \begin{center}
    \begin{table}[ht]
    \caption{Virtual Machine Byte Code}\label{tab:vm}
    \begin{tabular}{lll}
    \toprule
         {\bf Group} & {\bf Name} & {\bf Parameters} \\
         \midrule\midrule[.1em]

     \multirow{4}{2.5cm}[-1.5ex]{ Stack} 
        & {\tt push}
        & {\tt store} \\
     \cmidrule(lr){2-3}
        & {\tt pop}
        & {\tt success} \\
     \cmidrule(lr){2-3}
        & {\tt load}
        & {\tt success} \\    
     \cmidrule(lr){2-3}
        & {\tt save}
        & {\tt success} \\   
    \midrule[.1em] 

     \multirow{4}{2.5cm}[-1.5ex]{Stack} 
        & {\tt push}
        & {\tt store} \\
     \cmidrule(lr){2-3}
        & {\tt pop}
        & {\tt success} \\
     \cmidrule(lr){2-3}
        & {\tt load}
        & {\tt success} \\    
     \cmidrule(lr){2-3}
        & {\tt save}
        & {\tt success} \\   

    \bottomrule
    \end{tabular}
    \end{table}
    \end{center}
\end{document}

Some Suggestions

The look of the table could be improved a bit. Don't use \begin{centering}, it adds some unnecessary vertical space. Also, we probably don't need all of the rules. Especially double rules. To seperate some rows, which might otherwise be difficult to distinguish, it could be better to simply add some space with \addlinespace from booktabs-package. Also, as others have pointed out, use array-package so you don't need to write the same thing so many times. For the header you can add a command which sets the header-format.

Also, is seems, that as I have made the changes with removing the rules and inserting the space, there is no longer any use of the fixup-parameter as it has centered nicely, so I just removed it.

Output

enter image description here

Code

\documentclass[12pt]{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array}
\newcommand{\headerFormat}{\normalfont\bfseries}
\begin{document}
    \begin{table}[ht]
    \centering
    \caption{Virtual Machine Byte Code}\label{tab:vm}
    \begin{tabular}{>{\ttfamily}lll}
    \toprule
         \headerFormat Group & \headerFormat Name & \headerFormat Parameters \\
         \midrule
     \multirow{4}{2.5cm}{Stack} 
        & push
        & store \\
        & pop
        & success \\
        & load
        & success \\    
        & save
        & success \\   
   \addlinespace
     \multirow{4}{2.5cm}{Stack} 
        & push
        & store \\
        & pop
        & success \\
        & load
        & success \\    
        & save
        & success \\   
    \bottomrule
    \end{tabular}
    \end{table}
\end{document}
| improve this answer | |
1

Some improvements:

Load caption to have a decent spacing between caption and tabular (LaTeX doesn't swap the values of \abovecaptionskip and \belowcaptionskip when caption is above the float). Don't use a center environment with floats, as it adds unwanted vertical spacing. Use \centering instead, inside the table environment. Don't use \bf, \tt, &c., but \bfseries or \textbf, \ttfamily or \texttt. I added a small correction to \multirow for vertical centring of the contents.

\documentclass[12pt]{article}
\usepackage{caption} \usepackage{booktabs}
\usepackage{multirow}

\begin{document}


    \begin{table}[ht]
\centering
    \caption{Virtual Machine Byte Code}\label{tab:vm}
    \begin{tabular}{lll}
    \toprule
         \textbf{Group} & \textbf{Name} & \textbf{Parameters} \\
         \midrule\midrule[.1em]
     \multirow{4}{2.5cm}[-1.5ex]{\centering Stack}
        & \texttt{push}
        & \texttt{store} \\
     \cmidrule(lr){2-3}
        & \texttt{pop}
        & \texttt{success} \\
     \cmidrule(lr){2-3}
        & \texttt{load}
        & \texttt{success} \\
     \cmidrule(lr){2-3}
        & \texttt{save}
        & \texttt{success} \\
    \midrule[.1em]
     \multirow{4}{2.5cm}[-1.5ex]{\centering Stack}
        & \texttt{push}
        & \texttt{store} \\
     \cmidrule(lr){2-3}
        & \texttt{pop}
        & \texttt{success} \\
     \cmidrule(lr){2-3}
        & \texttt{load}
        & \texttt{success} \\
     \cmidrule(lr){2-3}
        & \texttt{save}
        & \texttt{success} \\
    \bottomrule
    \end{tabular}
    \end{table}

\end{document} 

enter image description here

| improve this answer | |

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.