2

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{.4\textwidth}p{.6\textwidth}}
\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
\setlength\parindent{0pt} % no indent
4
  • 1
    Horizontal spaces are inserted before and after every column, each has width of .5\tabcolsep. So you need to specify smaller width for those to p{...} columns. Jul 23, 2020 at 2:55
  • You mean the space between p{.4\textwidth} & p{.6\textwidth}? Jul 23, 2020 at 3:17
  • I removed the space but it still the same, no change. Jul 23, 2020 at 3:30
  • To fix my previous comment: It should be \tabcolsep, not .5\tabcolsep. Jul 23, 2020 at 5:10

3 Answers 3

4

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

You specified

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

The usable overall width is 1.0\textwidth. However, because of padding on both sides of both columns (in the amount of \tabcolsep), the total overall width is 1.0\textwidth+4\tabcolsep. 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.4\textwidth-2\tabcolsep\relax}
                  p{\dimexpr.6\textwidth-2\tabcolsep\relax}}

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

\begin{longtable}{@{}p{\dimexpr.4\textwidth-\tabcolsep\relax}
                     p{\dimexpr.4\textwidth-\tabcolsep\relax} @{}}
7
  • Thanks a lot Mico, it works perfectly. One more thing, is there any way to get the columns center aligned instead of left aligned keeping the exact same textwidth as here? Jul 23, 2020 at 6:02
  • @MAzharulIslam - Certainly: be sure to load the array package and to replace p{...} with >{\centering\arraybackslash}p{...}.
    – Mico
    Jul 23, 2020 at 10:50
  • ,Can You help me about another issue I am having in the same table, please? I am using \makecell in the first column, in the first two rows it's behaving ok, but from the third row, it's adding a space in the first line making the sentence starts from the second line. How to remove this first line spacing Jul 24, 2020 at 5:47
  • @MAzharulIslam - I'm afraid you haven't provided nearly enough information to allow a diagnosis of what may be going on. May I suggest that you post a new query, with a stylized example of what's going wrong? Thanks.
    – Mico
    Jul 24, 2020 at 6:26
  • I am using \makecell[t]{abc\\def\\syz} Jul 24, 2020 at 6:33
4

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=xxx\hsize}, 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.8\hsize\centering}X|>{\hsize=1.2\hsize\centering\arraybackslash}X}
 \toprule
  example letter & abc... \\ \midrule
\end{xltabular}

\end{document}  

enter image description here

1
  • thanks. It's working great. Have a good day Jul 23, 2020 at 9:38
0

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{.35\textwidth} p{.55\textwidth}}
                    \hline
                    example letter & abc... \\ \hline
\end{tabular}
2
  • It's not working, I tried to reduce the total \textwidth to .9. Jul 23, 2020 at 3:59
  • please upload your code as a MWE in your original question
    – js bibra
    Jul 23, 2020 at 4:01

You must log in to answer this question.

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