5

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.

3 Answers3

5

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.

Werner
  • 603,163
  • 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 09 '17 at 02:33
  • Hi Werner, if i change the column count , would it accommodate the column width automatically? – vincent911001 Mar 09 '17 at 02: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 09 '17 at 02: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 09 '17 at 03:00
  • @vincent911001: Sure. Add the calc package to your preamble and then calculate the length: \newlength{\mylen} \setlength{\mylen}{\linewidth / 12}. Now you can use *{12}{p{\dimexpr\mylen-2\tabcolsep}}. – Werner Mar 09 '17 at 03:05
  • 1
    You don't need \dimexpr if you use calc. – cfr Mar 09 '17 at 03:12
  • @Werner, i am experiencing text overflowing in longtable when using \newline. Is it approriate for me to ask here or start another thread? Besides, is it possible to get the solution without switching to another tabular package? – vincent911001 Mar 09 '17 at 03:40
  • @Werner, thanks alot for your help from another post which solves this problem using \seqsplit. @Werner and @cfr, thanks alot guys. – vincent911001 Mar 09 '17 at 03:45
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

cfr
  • 198,882
  • Hi cfr, thanks a lot for your answer. Wish i could accept both answers. – vincent911001 Mar 09 '17 at 03:07
  • @vincent911001 Please see edit. – cfr Mar 09 '17 at 03: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 09 '17 at 03: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 09 '17 at 03: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 09 '17 at 03:49
  • @vincent911001 You can only ping one person at a time. If you'd pinged me in your comment on Werner's post, though, Werner would also get pinged as it is Werner's post. But if you explicitly ping Werner, I don't get pinged, too, it seems. – cfr Mar 09 '17 at 03:52
  • thanks for the tips, noted and thanks a lot for your elegant solution as i can write less code using yours solution. – vincent911001 Mar 09 '17 at 04:55
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

Bernard
  • 271,350