La columna de la tabla no encaja

Aug 21 2020

Estoy usando el paquete tabularx y, según la respuesta anterior, esto debería solucionar el problema de tener una tabla donde las filas son demasiado anchas. Según tengo entendido, esto envolverá los textos y encajará. Pero en realidad esto va más allá del ancho de la página.

\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}

Respuestas

4 Zarko Aug 21 2020 at 12:55

El texto textual es demasiado largo para caber en la tabla. Puedo ser una solución que, en su lugar \verb, use la \ttfamilyfuente y habilite, que el texto en la cabina de la segunda columna se divida en más líneas:

\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}
3 leandriis Aug 21 2020 at 12:56

Si echa un vistazo más de cerca a las advertencias que recibe al compilar su documento, puede encontrar

\verb may be unreliable inside tabularx

Esto ya da una pista de por qué su tabla aún se desborda en los márgenes, aunque usó tabularx. Para superar esto, puede usarlo \lstinlineen combinación con la breaklines=trueopción del listingspaquete, que ya carga en su preámbulo. Por supuesto, la fuente que se usa para el código se puede ajustar usando las opciones apropiadas dentro de \lstset.

El siguiente MWE contiene dos versiones diferentes de su tabla, una que usa tabularx, la otra normal tabularcon pcolumnas de tipo. Ambos son tan anchos como el ancho del texto. En el ejemplo, también comenté los paquetes innecesarios y agregué una explicación de por qué eliminé cada paquete.

\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}