2

I used the following code to create a longtable with five columns that stretches the full width of the page. I like everything about this table except for the fact that all columns are padded to make the table as wide as the page. Instead, I would like the first four columns to expand in this way, but I would like the fifth column (the one with header D) to be "sized to content". That is, I would like it to be only as wide as it needs to be to fit its values.

\documentclass[table]{article}

\usepackage{fancyhdr}
\usepackage[left=0.50in,top=2.0in,right=0.50in,bottom=1.0in]{geometry}
\setlength\parindent{0pt}

\usepackage{longtable}
\usepackage{pdflscape}

\begin{document}

\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}{l @{\extracolsep{\fill}} |   r|r|r|r}

        \multicolumn{5}{r}{\bfseries {\Large{My Table}}} \\

       & {\bfseries A}
       & {\bfseries B}
       & {\bfseries C}
       & {\bfseries D}
       \\
       \endfirsthead

        \multicolumn{5}{r}{\bfseries {\Large{My Table (continued)}}} \\

           % name
       & {\bfseries A}
       & {\bfseries B}
       & {\bfseries C}
       & {\bfseries D}
       \\
       \hline
       \endhead

            \hline

               \hspace{4mm}\ Row 1 \hspace{2mm}
            & 1,200
            & 800
            & 2,000
            & 1
            \\

               \hspace{4mm}\ Row 2  \hspace{2mm}
            & 1,201
            & 801
            & 2,001
            & 123
            \\

\end{longtable}

\end{document}

How it currently looks:

enter image description here

My crude approximation of how it should look:

enter image description here

Is there a way to do this?

3
  • Welcome to TeX.SE. – Mico Nov 25 '20 at 5:44
  • As always on the site, please provide a full minimal example, not sniplets. With a full but minimal self contained example it is a lot easier for others to copy your code and test it as is. Here we have to add stuff to even compile it, so a lot of people will not even bother to look at it. – daleif Nov 25 '20 at 7:08
  • I have updated the code to show the full document, rather than just a snippet. Thank you. – ngm Nov 25 '20 at 14:55
0

Like this:

enter image description here

\documentclass{article}

%\usepackage{fancyhdr}
\usepackage[hmargin=0.50in,top=2.0in,bottom=1.0in]{geometry}
\setlength\parindent{0pt}

\usepackage{array, longtable}
%\usepackage{pdflscape}

\begin{document}

%\setlength\LTleft{0pt}
%\setlength\LTright{0pt}
\begin{longtable}{@{\qquad}l| *{3}{>{\raggedleft}p{0.25\linewidth}|} r}

        \multicolumn{5}{r}{\bfseries {\Large{My Table}}} \\

       & {\bfseries A}
       & {\bfseries B}
       & {\bfseries C}
       & {\bfseries D}
       \\
       \endfirsthead

        \multicolumn{5}{r}{\bfseries {\Large{My Table (continued)}}} \\

           % name
       & {\bfseries A}
       & {\bfseries B}
       & {\bfseries C}
       & {\bfseries D}
       \\
       \hline
       \endhead

            \hline

Row 1       & 1,200
            & 800
            & 2,000
            & 1
            \\

Row 2       & 1,201
            & 801
            & 2,001
            & 123
            \\

\end{longtable}

\end{document}
1
  • Works great, thank you! – ngm Nov 25 '20 at 21:56

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.