# Table column does not fit

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}{>{\RaggedRight\hangafter=1\hangindent=1.5em}X}

\vspace{1cm}
% \newcolumntype{b}{X}
% \newcolumntype{s}{>{\hsize=.5\hsize}X}
\begin{table*}
\caption{example}
\label{tab:code-example}
\centering
% \begin{tabularx}{\textwidth}{XX}
\begin{tabularx}{1.0\textwidth}{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}

• First off, your code currently contains a syntax error: \hline must occur inside a tabular-like or array-like environment; did you maybe mean to run \hrule? Second, the use of \verb|...| in table's second column basically defeats the purpose of using X columns. Please explain why you're using \verb, i.e., what it is that you ultimately wish to accomplish in terms of formatting. – Mico Aug 21 '20 at 5:13
• I am trying to do inline code listing in with the \verb, that's the reason I am using \verb. I like the way code is displayed with \verb, that's the sole reason of using verb – Exploring Aug 21 '20 at 5:16
• Unrelated to the question itself, but please clean up your preamble. Currently, you load several packages more than once. Please remove the duplicates. – leandriis Aug 21 '20 at 5:19
• tex.stackexchange.com/questions/436383/… might be interesting to have a look at. – leandriis Aug 21 '20 at 5:21

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.:

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

\begin{document}

\listoftables

\begin{table}[ht]
\caption{example}
\label{tab:code-example}
\centering
\renewcommand{\arraystretch}{1.3}
\begin{tabularx}{\textwidth}{@{}>{\hsize=0.8\hsize}L
>{\hsize=1.2\hsize\ttfamily}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}

• here you have used hsize 0.8 and 1.2, together they are 0.8+1.2=2, should the parts match up to 1? Sorry I just trying to understand/ – Exploring Aug 21 '20 at 17:44
• @Exploring, sum had to be (i) integer, and (ii) equal to number of the X columns in table. For details see tabularx documentation, subsection 4.3 Column widths on page 3. – Zarko Aug 21 '20 at 18:45

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.

\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.0\textwidth}{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}{>{\raggedright\arraybackslash}p{\dimexpr0.35\linewidth-2\tabcolsep}
>{\raggedright\arraybackslash}p{\dimexpr0.65\linewidth-2\tabcolsep}}
\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}