TransWikia.com

Table column does not fit

TeX - LaTeX Asked on July 18, 2021

I am using the tabularx package and as per previous answer this should sort by problem of having a table where rows are too wide. My understanding was this will wrap the texts and it would fit in. But actually this is going beyond the page width.

documentclass{article}
usepackage[utf8]{inputenc}
usepackage{array}
usepackage{wrapfig}
usepackage{multirow}
usepackage{tabularx}% for 'tabularx' env. and 'X' col. type
usepackage{lscape} 
usepackage{listings}
usepackage{color}
usepackage{xparse}
usepackage{placeins}
usepackage{caption} 
usepackage{enumitem}
usepackage{booktabs} % for toprule, midrule etc macros
usepackage{ragged2e} % for RaggedRight macro


title{examle}

author{An example from Overleaf}

begin{document}

maketitle
renewcommand{arraystretch}{1.3}

listoftables

%% create a derivative column type called 'L':
newcolumntype{L}{>{RaggedRighthangafter=1hangindent=1.5em}X}

vspace{1cm}
% newcolumntype{b}{X}
% newcolumntype{s}{>{hsize=.5hsize}X}
begin{table*}
    caption{example}
    label{tab:code-example}
    centering
    % begin{tabularx}{textwidth}{XX}
    begin{tabularx}{1.0textwidth}{XX}
    toprule  % replaced all hline commands with rules from the booktabs package
    upshape  Representation of the code & Code     
    midrule
Representation 1    
& verb|ID math . ID1 calculateSomethingAwesome ( LIT 100 , LIT 200, LIT 300 )|  
Another representation name that can be very very long
& verb|ID math . ID1 calculateSomethingAwesome ( LIT 100 , INTEGER 200, LIT 300 )|  
Representation 3 
& verb|ID math . ID1 calculateSomethingAwesome ( FLOAT 100 , NUMERIC 200, LIT ID 300 )| 
    bottomrule
    end{tabularx}
end{table*} 



end{document}

2 Answers

The verbatim text is to long that can be fit in table. I may be solution that instead verb you use ttfamily font and enable, that text in second column cab be broken in more lines.:

enter image description here

documentclass{article}
usepackage{ragged2e}
usepackage{booktabs, tabularx}
newcolumntype{L}{>{RaggedRighthangafter=1hangindent=1em}X}

begin{document}

listoftables

    begin{table}[ht]
caption{example}
label{tab:code-example}
    centering
renewcommand{arraystretch}{1.3}
begin{tabularx}{textwidth}{@{}>{hsize=0.8hsize}L
                                >{hsize=1.2hsizettfamily}L
                             @{}}
    toprule  %
Representation of the code & Code   
    midrule
Representation 1
    & ID math.ID1 calculateSomethingAwesome (LIT 100, LIT 200, LIT 300)
Another representation name that can be very very long
    & ID math.ID1 calculateSomethingAwesome (LIT 100, INTEGER 200, LIT 300) 
Representation 3
    & ID math.ID1 calculateSomethingAwesome (FLOAT 100, NUMERIC 200, LIT ID 300)
    bottomrule
end{tabularx}
    end{table}
end{document}

Correct answer by Zarko on July 18, 2021

If you take a closer look at the warnings you get upon compiling your document, you can find

verb may be unreliable inside tabularx

This already gives a hint on why your table still overflows into the margins, although you used tabularx. To overcome this, you can use lstinline in combination with the breaklines=true option from the listings package, which you already load in your preamble. The font that is used for the code can of course be adjusted using appropriate options inside of lstset.

The following MWE contains two different versions of your table, one using tabularx, the other a regular tabular with p type columns. Both are as wide as the textwidth. In the example, I have also commented out unneccessary packages and added an explanation why I removed each package.

enter image description here

documentclass{article}
%usepackage[utf8]{inputenc} not needed since this is the default with an up-to-date installation
%usepackage{array} not needed since internally loaded by tabularx
usepackage{wrapfig}
usepackage{multirow}
usepackage{tabularx}
%usepackage{array,booktabs} Do not load packages more than once.
usepackage{lscape} 
usepackage{listings}
%usepackage{color} better to use xcolor instead
%usepackage{tabularx} Do not load packages more than once.
%usepackage{multirow} Do not load packages more than once.
%usepackage{lscape}  Do not load packages more than once.
usepackage{xparse}
usepackage{placeins}
usepackage{caption} 
usepackage{enumitem}
%usepackage{array} Do not load packages more than once.
%usepackage{wrapfig} Do not load packages more than once.
%usepackage{booktabs} Do not load packages more than once.
%usepackage{tabularx} Do not load packages more than once.
usepackage{ragged2e} % for RaggedRight macro
usepackage{booktabs}


lstset{breaklines=true}

title{ContextML Tables For the ICSE Submission}

author{An example from Overleaf}

begin{document}

maketitle
renewcommand{arraystretch}{1.3}

listoftables


begin{table}
    caption{example}
    label{tab:code-example}
    centering
    begin{tabularx}{1.0textwidth}{XX}
    toprule  % replaced all hline commands with rules from the booktabs package
    upshape  Representation of the code & Code     
    midrule
Representation 1    
& {lstinline|ID math . ID1 calculateSomethingAwesome ( LIT 100 , LIT 200, LIT 300 )|}  
Another representation name that can be very very long
& {lstinline|ID math . ID1 calculateSomethingAwesome ( LIT 100 , INTEGER 200, LIT 300 )|}  
Representation 3 
& {lstinline|ID math . ID1 calculateSomethingAwesome ( FLOAT 100 , NUMERIC 200, LIT ID 300 )|} 
    bottomrule
    end{tabularx}
end{table} 


begin{table}
    caption{example}
    label{tab:code-example}
    centering
    begin{tabular}{>{raggedrightarraybackslash}p{dimexpr0.35linewidth-2tabcolsep}
                    >{raggedrightarraybackslash}p{dimexpr0.65linewidth-2tabcolsep}}
    toprule  % replaced all hline commands with rules from the booktabs package
    upshape  Representation of the code & Code     
    midrule
Representation 1    
& {lstinline|ID math . ID1 calculateSomethingAwesome ( LIT 100 , LIT 200, LIT 300 )|}  
Another representation name that can be very very long
& {lstinline|ID math . ID1 calculateSomethingAwesome ( LIT 100 , INTEGER 200, LIT 300 )|}  
Representation 3 
& {lstinline|ID math . ID1 calculateSomethingAwesome ( FLOAT 100 , NUMERIC 200, LIT ID 300 )|} 
    bottomrule
    end{tabular}
end{table} 


end{document}

Answered by leandriis on July 18, 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