TransWikia.com

How to make a landscape table fill the whole width?

TeX - LaTeX Asked by xport on February 16, 2021

It seems that neither textwidth nor textheight can force the table to fill the whole width.

documentclass[dvips]{book}
usepackage[a4paper,margin=20mm,showframe]{geometry}
usepackage{lscape}
usepackage{lipsum}
usepackage{calc}
usepackage{longtable,array}

begin{document}
chapter{One}
lipsum[1]
section{One One}
lipsum[2]

pagestyle{empty}
begin{landscape}
begin{longtable}{|m{0.4textheight-2tabcolsep-1.5fboxrule}|m{0.6textheight-2tabcolsep-1.5fboxrule}|}
hline
a & b\hline
c & d\hline
end{longtable}
end{landscape}
pagestyle{plain}

lipsum[3]
end{document}

EDIT 1:

I made a correction in the column width, i.e., changing 2fboxrule to 1.5fboxrule. The former calculation is wrong because the rule between 2 columns should NOT be calculated twice. I slipped a little here.

And I got an output as follows:

alt text


EDIT 2: It’s my real scenario (as opposed to the minimal code)

documentclass[dvipsnames,rgb,dvips]{book}

usepackage[a4paper,margin=20mm,showframe]{geometry}
usepackage{longtable}
usepackage{array}
usepackage{calc}
usepackage{ltablex}
usepackage{lscape}

newcounter{No}
renewcommand{theNo}{stepcounter{No}arabic{No}}
newenvironment{MyTable}[4]%
{%
    newcolumntype{A}[1]%
    {%
        >{%
            begin{minipage}%
                {%
                    ##1textwidth-2tabcolsep-1.5arrayrulewidth%
                }%
                vspace{tabcolsep}%
         }%
         c%
        <{vspace{tabcolsep}end{minipage}}%
    }%
    setcounter{No}{0}%comment out this if you want to continuous numbering for all tables.
    begin{longtable}%
    {%
            |>{scriptsizecenteringtheNo}A{#1}<{}%
            |>{}A{#2}<{centeringinput{jobname.tmp}}%
            |>{centeringlstinputlisting{jobname.tmp}}A{#3}<{}%
            |>{scriptsizearraybackslash}A{#4}<{}%
            |%
    }%
    hlineignorespaces%
}%
{%
    end{longtable}%
}

newcommand{Comment}[1]{& & & #1\hline}


usepackage{listings}
lstset{%
language={PSTricks},
basicstyle=ttfamilyscriptsize,%
keywordstyle=color{blue}%,
%backgroundcolor=color{yellow!30}%
}
usepackage{fancyvrb}


defMyRow{%        
        VerbatimEnvironment%
        begin{VerbatimOut}{jobname.tmp}%
}

defendMyRow{%
        end{VerbatimOut}%      
}



usepackage{pstricks,pst-node}
newpsstyle{gridstyle}{%
gridwidth=0.4pt,%default: 0.8pt
gridcolor=Red!20,%default: black
griddots=0,%default: 0 
%
gridlabels=3pt,%default: 10pt
gridlabelcolor=Blue,%default: black
%
subgriddiv=5,%default: 5
subgridwidth=0.2pt,%default: 0.4pt
subgridcolor=Green!20,%default: gray
subgriddots=0%default: 0
}

usepackage{lipsum}

begin{document}
chapter{Introduction to PSTricks}
lipsum[1]

section{At a glance galleries}
lipsum[1-5]

%Landscape starts here.
%pagestyle{empty}
begin{landscape}
thispagestyle{empty}
begin{MyTable}{0.05}{.25}{0.4}{0.3}%
%=============
begin{MyRow}
pspicture*[showgrid](4,4)
pnode(1,1){A}
pnode(3,3){B}
ncline{A}{B}
endpspicture
end{MyRow}
Comment{lipsum[1]}
%=============
begin{MyRow}
begin{pspicture}[showgrid](4,3)
psframe*[linecolor=red!30](3,2)
end{pspicture}
end{MyRow}
Comment{lipsum[2]}
%=============
begin{MyRow}
pspicture[showgrid](4,3)
psframe*[linecolor=green!30](3,2)
endpspicture
end{MyRow}
Comment{lipsum[3]}
%=============
begin{MyRow}
pspicture[showgrid](4,3)
psframe*[linecolor=yellow!30](3,2)
endpspicture
end{MyRow}
Comment{lipsum[4]}
%=============
begin{MyRow}
pspicture[showgrid](4,3)
psframe*[linecolor=Maroon!30](3,2)
endpspicture
end{MyRow}
Comment{lipsum[5]}
%=============
end{MyTable}
%Landscape stops here.
end{landscape}
%pagestyle{plain}

section{Node}
lipsum[1-5]
end{document}

Unfortunately, arrayrulewidth does not work.


EDIT 3:

dvips has been added for both code snippets above. The arrayrulewidth still cannot work the way we want.

4 Answers

Here is my solution

documentclass[dvipsnames,rgb,dvips]{book}

usepackage[a4paper,margin=20mm,showframe]{geometry}
usepackage{longtable}
usepackage{array}
usepackage{calc}
usepackage{ltablex}
usepackage{lscape}
usepackage{ragged2e}

newcounter{No}
renewcommand{theNo}{stepcounter{No}arabic{No}}
newcolumntype{A}[1]{%
  >{vspace*{medskipamount}Centering}
  p{#1linewidth-2tabcolsep-2arrayrulewidth}
  <{smallskip}}
newenvironment{MyTable}[4]%
  {setcounter{No}{0}%comment out this if you want to continuous numbering for all tables.
   begin{longtable}{%
            |>{scriptsizetheNo}A{#1}<{}%
            |>{}l<{minipage[t]{#2linewidth-2tabcolsep-2arrayrulewidth}
                   vspace{0pt}input{jobname.tmp}endminipage}%
            |>{Centeringlstinputlisting{jobname.tmp}}A{#3}<{}%
            |>{scriptsizearraybackslash}A{#4}<{}%
            |%
    }%
    hlineignorespaces%
}%
{%
    end{longtable}%
}

newcommand{Comment}[1]{& & & #1\hline}

usepackage{listings}
lstset{%
language={PSTricks},
basicstyle=ttfamilyscriptsize,%
keywordstyle=color{blue}%,
%backgroundcolor=color{yellow!30}%
}
usepackage{fancyvrb}
defMyRow{%        
        VerbatimEnvironment%
        begin{VerbatimOut}{jobname.tmp}}

defendMyRow{end{VerbatimOut}}

usepackage{pstricks,pst-node}
newpsstyle{gridstyle}{%
gridwidth=0.4pt,%default: 0.8pt
gridcolor=Red!20,%default: black
griddots=0,%default: 0 
%
gridlabels=3pt,%default: 10pt
gridlabelcolor=Blue,%default: black
%
subgriddiv=5,%default: 5
subgridwidth=0.2pt,%default: 0.4pt
subgridcolor=Green!20,%default: gray
subgriddots=0%default: 0
}

usepackage{lipsum}

begin{document}
chapter{Introduction to PSTricks}
lipsum[1]

section{At a glance galleries}
lipsum[1-5]

%Landscape starts here.
%pagestyle{empty}
begin{landscape}
noindent
rule{linewidth}{4pt}

thispagestyle{empty}
begin{MyTable}{0.05}{.25}{0.4}{0.3}%
%=============
begin{MyRow}
pspicture[showgrid](4,4)
pnode(1,1){A}
pnode(3,3){B}
ncline{A}{B}
endpspicture
end{MyRow}
Comment{lipsum[1]}
%=============
begin{MyRow}
begin{pspicture}[showgrid](4,3)
psframe*[linecolor=red!30](3,2)
end{pspicture}
end{MyRow}
Comment{lipsum[2]}
%=============
begin{MyRow}
pspicture[showgrid](4,3)
psframe*[linecolor=green!30](3,2)
endpspicture
end{MyRow}
Comment{lipsum[3]}
%=============
begin{MyRow}
pspicture[showgrid](4,3)
psframe*[linecolor=yellow!30](3,2)
endpspicture
end{MyRow}
Comment{lipsum[4]}
%=============
begin{MyRow}
pspicture[showgrid](4,3)
psframe*[linecolor=Maroon!30](3,2)
endpspicture
end{MyRow}
Comment{lipsum[5]}
%=============
end{MyTable}
%Landscape stops here.
end{landscape}
%pagestyle{plain}

section{Node}
lipsum[1-5]
end{document}

Correct answer by user2478 on February 16, 2021

I found a very simple solution: Just use

resizebox{linewidth}{!}{
begin{tabular}{}
%Your table content
end{tabular}
}

Apparently linewidth is redefined by landscape but textwidth not.

Answered by pmaen on February 16, 2021

Disclaimer: This solution is intended for KOMA-script's classes only.


The landscape environment of the pdflscape package is indeed the key for creating sideway floats, as shown in Herbert's solution.
Indeed, it rotates the float but keeps head- and footnotes correctly positioned. Moreover, it automatically rotates the relevant page in the pdf, in order to make easy to read the document on screen.

However, as noted in KOMA-Script documentation in §16.6:

Package lscape defines an environment landscape to set the page contents but not head and foot landscape. Inside this environment it changes textheight to the value of textwidth, but it does not change textwidth to the former value of textheight. This is inconsistent.

The solution for KOMA-script's classes is thus to also load the scrhack package, after pdflscape.

enter image description here

enter image description here


documentclass{scrartcl}
    %needed for this solution
    usepackage{pdflscape}
    usepackage{scrhack}

    %mainstream packages for tables
    usepackage{tabularx}
    usepackage{booktabs}

    %for illustration purpose
    usepackage{lipsum}
    usepackage{showframe}
begin{document}
    lipsum[1-2]

    begin{landscape}%
        captionof{table}[Caption for LoT]{Caption of the table}%
        small%
        noindent%
        begin{tabularx}{textheight}{lXX}
        toprule
            emph{Lorem}
            &
            textbf{Ipsum dolor}
            &
            textbf{Sit amet}
        \
        midrule
            emph{Consectetur adipiscing}
            &
            In sed facilisis lacus, consectetur volutpat nulla. Praesent quis vulputate urna.
            &
            Suspendisse non dui fermentum, convallis velit non, consequat diam.
        \
            emph{In at sem eros.}
            &
            Maecenas tellus eros, porttitor non varius eget, porttitor eu ipsum.
            &
            Etiam accumsan pharetra convallis. 
        \
        bottomrule
        end{tabularx}%
    end{landscape}

    lipsum[3-4]
end{document}

Answered by ebosi on February 16, 2021

always use linewidth inside the landscape environment and not textwidth

Answered by user2478 on February 16, 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