TransWikia.com

Table out of margin while using 'p'

TeX - LaTeX Asked on December 5, 2021

Any table build with begin{tabular}{p{textwidth}……} goes out of margin.
An example table is

documentclass[12pt,a4paper]{report}

usepackage{booktabs} 
usepackage{tabularx}
usepackage{longtable}
usepackage{makecell}

begin{document}

begin{longtable}{p{.4textwidth}p{.6textwidth}}
hline
example letter & abc... \ hline
end{longtable}

end{document}

enter image description here

I am writing my thesis which is a big document with multiple chapters. The preamble of the document looks like this.

documentclass[12pt,a4paper]{report}
usepackage{amsmath,amssymb,amsfonts} % for mathematical operations
allowdisplaybreaks %allow align to break in pages
usepackage[top=2.54cm, bottom=2.54cm, left=3.56cm, right=2.54cm]{geometry} % margin
usepackage{graphicx, epstopdf} % insert images
graphicspath{{figures/}} % image path
usepackage[section]{placeins} % figure placement
usepackage[framed, numbered]{matlab-prettifier} % to insert matlab code
usepackage[labelfont=bf, labelsep=space]{caption} % caption formatting
usepackage{subcaption} % subcaption for subfigure
usepackage{lipsum}
usepackage{booktabs} % for book tabs type table, toprule...
usepackage{tabularx}
usepackage{longtable}
usepackage{makecell} %break cell in table
usepackage[T1] {fontenc} % better pdf output
usepackage{textcomp}
usepackage{enumitem} % for controlling item sepaeration
usepackage{IEEEtrantools} % for using IEEEtran tools in this document 
usepackage{setspace} % different spacing and stretch
usepackage{xcolor} % for using colors
usepackage{times} % times new roman font for the whole document
usepackage{anyfontsize} % any font size can be used with instead built in large type
usepackage{nameref}
usepackage{float} % uses [H] for figures and tables alignment
usepackage{url} % for using url in the references
setlengthparindent{0pt} % no indent

3 Answers

Why not use xltabular? It combines the features of longtable and tabularx. To have X columns in a certain ratio, we use the syntax >{hsize=xxxhsize}, where the coefficients are in the desired ratio, and the sum of the coefficients is the total number of X columns.

documentclass[12pt, a4paper]{report}

usepackage{booktabs}
usepackage{xltabular}
usepackage{longtable}
usepackage{makecell}
usepackage{lipsum}

begin{document}

lipsum[11]
begin{xltabular}{linewidth}{>{hsize=0.8hsizecentering}X|>{hsize=1.2hsizecenteringarraybackslash}X}
 toprule
  example letter & abc... \ midrule
end{xltabular}

end{document}  

enter image description here

Answered by Bernard on December 5, 2021

(too long for a comment, hence posted as an answer)

You specified

begin{longtable}{p{.4textwidth}p{.6textwidth}}

The usable overall width is 1.0textwidth. However, because of padding on both sides of both columns (in the amount of tabcolsep), the total overall width is 1.0textwidth+4tabcolsep. The default value of tabcolsep in many document classes, including report, is 6pt. Hence, the longtable's total width exceeds textwidth by 24pt, or roughly one third of an inch (or ca 8.5mm, if you prefer).

To fix this, you should write either

begin{longtable}{p{dimexpr.4textwidth-2tabcolseprelax}
                  p{dimexpr.6textwidth-2tabcolseprelax}}

or, if you don't need the padding at the left- and right-hand edges,

begin{longtable}{@{}p{dimexpr.4textwidth-tabcolseprelax}
                     p{dimexpr.4textwidth-tabcolseprelax} @{}}

Answered by Mico on December 5, 2021

Some space requires to be left for the separators between each column-- hence the reduction to 0.35 and 0.55

enter image description here

begin{tabular}{p{.35textwidth} p{.55textwidth}}
                    hline
                    example letter & abc... \ hline
end{tabular}

Answered by js bibra on December 5, 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