3

I want to put all elements of the array vertically centered. I have tried \arraystretch command to do so, but it is not working.

Code:

\documentclass[12 pt, a4paper]{book}
\usepackage{multicol}
\usepackage{geometry}
\geometry{
     a4paper,
     total={170 mm,257 mm},
     left=20 mm,
     top=20 mm,
     }
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{soul}
\usepackage{indentfirst}
\usepackage[utf8]{inputenc}
\usepackage{palatino}
\usepackage{color}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{enumitem}
\usepackage{mathtools}


\setlength{\parindent}{2em}

\begin{document}

\indent \textbf{Step 2 :} \hspace{0.2 cm} Find the hexa-decimal equivalent of the integer part.
    \[
    \hspace{-2 cm}      
    \begin{array}{c|c|c}
        \renewcommand{\arraystretch}{5}
        \textbf{Radix} & \textbf{Integer} & \textbf{Remainder}\\
        \hline
         16 & 620 & \mathrm{C}\\
        \hline
         16 &  38 & 6\\
        \hline
         16 &   2 & 2\\
        \hline
            &   0 &  \\
    \end{array}
    \]
\end{document}

The output obtained from this code is as follows:

Output:

Obtained output

Can anyone help me to get the elements of array vertically centered?

2 Answers 2

2

With use of \setlength\extrarowheight{2pt} numbers will looks better vertically centered:

enter image description here

\documentclass[12 pt, a4paper]{book}
\usepackage{array}

\setlength{\parindent}{2em}

\begin{document}

\indent \textbf{Step 2 :} \hspace{0.2 cm} Find the hexa-decimal equivalent of the integer part.
    \[\setlength\extrarowheight{2pt}  % <---
    %\hspace{-2 cm}
    \begin{array}{c|c|c}
        \renewcommand{\arraystretch}{5}
        \textbf{Radix} & \textbf{Integer} & \textbf{Remainder}\\
        \hline
         16 & 620 & \mathrm{C}\\
        \hline
         16 &  38 & 6\\
        \hline
         16 &   2 & 2\\
        \hline
            &   0 &  \\
    \end{array}
    \]
\end{document}
2
  • This helped me today. But, what can I do if I want the text to be aligned center automatically in all cells? Can I play with \setlength\extrarowheight{2pt}? Thx
    – Mikasa
    Sep 7, 2022 at 7:38
  • @Mikasa, For vertical centering are available many approaches. One of them is mentioned extrarowheight. If you placed in document preamble, it has effect in ask tables, You ay also consider tabularray package. For more, please ask a question, where you explain your problem.
    – Zarko
    Sep 7, 2022 at 21:00
1

Don't play with arraystretch to vertically centre the contents of cells: it increases asymetrically the spaces at the top and at the bottom of cells. Use cellspace instead, which enables you to define minimal vertical spacings at the top and bottom of cells in columns with specifier prefixed with the letter S (or C if you load siunitx).

Unrelated: needless to load amsfonts if you load amssymb and amsmath if you load amthtools.

\documentclass[12 pt, a4paper]{book}
\usepackage{multicol}
\usepackage{geometry}
\geometry{
     a4paper,
     total={170 mm,257 mm},
     left=20 mm,
     top=20 mm,
     }
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{soul}
\usepackage{indentfirst}
\usepackage[utf8]{inputenc}%
%\usepackage{palatino}
\usepackage{newpxtext, newpxmath}
\usepackage{color}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{enumitem}
\usepackage[math]{cellspace}
\setlength{\cellspacetoplimit}{6pt}
\setlength{\cellspacebottomlimit}{6pt}

\setlength{\parindent}{2em}

\begin{document}

    \[
    \hspace{-2 cm}
    \begin{array}{c|Sc|c}
        \textbf{Radix} & \textbf{Integer} & \textbf{Remainder}\\
        \hline
         16 & 620 & C\mathrm{C}\\
        \hline
         16 & 38 & 6\\
        \hline
         16 & 2 & 2\\
        \hline
            & 0 & \\
    \end{array}
    \]

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .