TransWikia.com

Centering contents seems not to work

TeX - LaTeX Asked by Serkan B. on July 21, 2021

I am trying to create a rather unusual table. The problem seems simple but I could not fix it although I have been trying for a week.

Here is the code:

documentclass{book}
usepackage{array}
newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}

begin{document}
    noindentbegin{tabular}{|*{29}{P{0.1cm}|}}
        hline
        Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}
        hline
    end{tabular}
end{document} 

Here is the result:

enter image description here

As can be seen, the letter “A” is pushed to right side. I would like it to be in the box (cell).
I appreciate any ideas that may help me understand the problem.

To illustrate further:

enter image description here

Here, the code is the same but letter size is tiny, with an additional row. Why aren’t the letters in the center? Where is that “space” between the left wall of the cell and the letter “A” coming from?

One Answer

Your columns are too narrow, use a c columns instead. However, then you will see that the table is too wide for the margins, even if you decrease tabcolsep to 2pt (or to 0pt).

Depending of your options, you may either put the page in landscape mode, using pdflscape, or rotate the tabular 90 degree. In Example 2, I have used rotatebox from the graphicx package to set the tabular in landscape mode. You also have to decrease the top an bottom margins to avoid overfull box warnings. I used geometry for that purpose.

If you need p columns, you can calculate the width using dimexpr or using tabularx. If do not you need multiline cells (i.e. p-columns), but all cells must be of same width, you can use array's w-columns and calculate the width.

Example 1 - c columns

enter image description here

documentclass{book}
usepackage{array}
%newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}
setlength{tabcolsep}{2pt}
setlength{extrarowheight}{1pt}

begin{document}
    noindentbegin{tabular}{|*{29}{c|}}
        hline
        Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}
        hline
    end{tabular}
end{document} 

Example 2 - rotatebox and c columns

enter image description here

documentclass{book}
usepackage{array}
usepackage[margin=2.5cm]{geometry}


usepackage{graphicx}

%newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}
setlength{tabcolsep}{2pt}
setlength{extrarowheight}{2pt}
renewcommand{arraystretch}{1.2}


begin{document}
pagestyle{empty}
noindentrotatebox [origin=center]{90}{%
begin{tabular}{|*{29}{c|}}
        hline
        Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}&Huge{A}
        hline
    end{tabular}%
}
end{document} 

Answered by Sveinung on July 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP