4

I have some questions regarding specifying column width in \longtable using \linewidth.

\documentclass[a4paper, 10pt]{article}
\usepackage[left=1in,right=1in,top=0.6in,bottom=0.6in]{geometry}
\usepackage{tabularx}
\usepackage {lipsum}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\rmdefault}{phv}
\usepackage{ ragged2e }
\usepackage{graphicx}
\usepackage{float}
\usepackage{longtable}
\usepackage{layouts}
\begin{document}
    \begin{longtable}
        {
            p{0.1\textwidth}
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth} 
            p{0.1\linewidth}
        }
        \hline
        a & a & a & a & a & a & a & a & a & a\\
        b & b & b & b & b & b & b & b & b & b\\
        \hline
    \end{longtable}
\end{document}

enter image description here

\documentclass[a4paper, 10pt]{article}
\usepackage[left=1in,right=1in,top=0.6in,bottom=0.6in]{geometry}
\usepackage{tabularx}
\usepackage {lipsum}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\rmdefault}{phv}
\usepackage{ ragged2e }
\usepackage{graphicx}
\usepackage{float}
\usepackage{longtable}
\usepackage{layouts}
\begin{document}
    \begin{longtable}{p{\linewidth}}
        \hline
        a\\
        \hline
    \end{longtable}
\end{document}

enter image description here

Thus, what should i do to fit x columns by dividing \linewidth evenly in a page?

Note: I have to use \longtable as the table might span multiple pages and i am using TeXstudio to compile my code.

  • Welcome! Please provide code we can compile. Do you need the table to run to multiple pages? – cfr Mar 9 '17 at 1:52
  • yes, i will update the code snippet in a moment – vincent911001 Mar 9 '17 at 1:52
4

Use the following simplified column specification

\begin{longtable}
  {
    *{10}{p{\dimexpr.1\linewidth-2\tabcolsep}}
  }

Each p-column has the \tabcolsep gap removed from both sides - the main cause for your table not fitting within the page margins. Also see My table doesn't fit; what are my options?

An alternative would be to use ltxtable which merges the use of longtable with that of tabularx, allowing you to specify X-type columns that naturally stretch to fit the remainder of the pre-specified longtable width.

  • Hi Werner, thanks a lot for your suggestion, i will go with the simplified column specification for now as the ltxtable requires table definition to be in different file. – vincent911001 Mar 9 '17 at 2:33
  • Hi Werner, if i change the column count , would it accommodate the column width automatically? – vincent911001 Mar 9 '17 at 2:39
  • 1
    @vincent911001: No. Since you've used 10 columns, I specified the column width as 1/10 of \linewidth. If you have (say) 12 columns, then use 0.8333\linewidth (or 1/12 of \linewidth). – Werner Mar 9 '17 at 2:57
  • Thanks Werner, i have tried that just now, now i am trying to dynamically adjust the column width by getting 1/(column count) * \linewidth. Is that possible in Latex? If not , then i will find my own ways of writing this out as i am generating this in my application. – vincent911001 Mar 9 '17 at 3:00
  • 1
    You don't need \dimexpr if you use calc. – cfr Mar 9 '17 at 3:12
4

The width of the columns given by p does not take account of the space between the columns and at the left and right of the first and final entries respectively.

You can either calculate a value yourself or use one of the packages which combine the facilities of tabularx with those of longtable. Here's the former approach:

\documentclass[a4paper,10pt]{article}
\usepackage[left=1in,right=1in,top=0.6in,bottom=0.6in,showframe]{geometry}
\usepackage{booktabs}
\usepackage{longtable,calc,array}
\newlength\mycolwidth
\begin{document}
\noindent
\setlength\mycolwidth{.1\linewidth-2\tabcolsep}%
\begin{longtable}{*{10}{>{\raggedright\arraybackslash}p{\mycolwidth}}}
    \toprule
    apple & apple & apple & apple & apple & apple & apple& apple & apple & apple\\
    \bottomrule
\end{longtable}
\end{document}

fitted columns

EDIT

Here's a way of calculating them automatically by using a custom longtablex environment.

\documentclass[a4paper,10pt]{article}
\usepackage[left=1in,right=1in,top=0.6in,bottom=0.6in,showframe]{geometry}
\usepackage{booktabs}
\usepackage{longtable,calc,array}
\newlength\mycolwidth
\newenvironment{longtablex}[1][10]{%
  \noindent
  \setlength\mycolwidth{\linewidth/#1-2\tabcolsep}%
  \begin{longtable}{*{#1}{>{\raggedright\arraybackslash}p{\mycolwidth}}}%
}{%
  \end{longtable}%
}
\begin{document}
\begin{longtablex}
    \toprule
    apple & apple & apple & apple & apple & apple & apple& apple & apple & apple\\
    \bottomrule
\end{longtablex}

\begin{longtablex}[5]
    \toprule
    apple & apple & apple & apple & apple \\
    \bottomrule
\end{longtablex}

\begin{longtablex}[3]
    \toprule
    apple & apple & apple  \\
    \bottomrule
\end{longtablex}
\end{document}

variations

  • Hi cfr, thanks a lot for your answer. Wish i could accept both answers. – vincent911001 Mar 9 '17 at 3:07
  • @vincent911001 Please see edit. – cfr Mar 9 '17 at 3:11
  • thanks for the extendable solution from you. But, can i know what this mean \newenvironment{longtablex}[1][10]{% \noindent \setlength\mycolwidth{\linewidth/#1-2\tabcolsep}% \begin{longtable}{*{#1}{>{\raggedright\arraybackslash}p{\mycolwidth}}}% }{% \end{longtable}% } – vincent911001 Mar 9 '17 at 3:15
  • i kind of understand the snippet, but can i know what is the usage of second parameter [10]. Is it a default column count? – vincent911001 Mar 9 '17 at 3:38
  • @vincent911001 It makes the single argument ([1]) optional by specifying the default, yes, as [10] for 10. So it means the environment can be used with no argument (for 10 columns) or with an argument in square brackets, which should specify the number of columns i.e. be a positive integer. If I just had [1] the argument would be required and would, therefore, go in curly brackets. – cfr Mar 9 '17 at 3:49
2

You also have the ltablex which brings the functionalities of longtable to tabularx. Also, I replaced the \hlines with rules from booktabs, to add some vertical padding around them:

\documentclass[a4paper, 10pt]{article}
\usepackage[hmargin=1in, vmargin=0.6in, showframe]{geometry}
\usepackage{ltablex, booktabs}
\usepackage {lipsum}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\rmdefault}{phv}
\usepackage{ ragged2e }
\usepackage{graphicx}
\usepackage{float}
\usepackage{layouts}
\keepXColumns

\begin{document}

    \begin{tabularx}{\linewidth}{*{10}{X}}
         \toprule
        a & a & a & a & a & a & a & a & a & a\\
        b & b & b & b & b & b & b & b & b & b\\
        \bottomrule
    \end{tabularx}
\end{document} 

enter image description here

  • thanks a lot for your solution and your help. Will definitely try it out. – vincent911001 Mar 10 '17 at 0:15

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.