表格列不适合

表格列不适合

我正在使用 tabularx 包,根据之前的回答,这应该根据表格行太宽的问题进行排序。我的理解是,这将换行文本,这样就适合了。但实际上这超出了页面宽度。

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

答案1

逐字文本太长,表格放不下。我可能的解决方案是\verb使用\ttfamily字体并启用,这样第二列中的文本就可以分成更多行。:

在此处输入图片描述

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

答案2

如果你仔细查看编译文档时收到的警告,你会发现

\verb may be unreliable inside tabularx

这已经暗示了为什么虽然使用了 ,但表格仍然溢出到边距tabularx。为了解决这个问题,您可以将与您已在序言中加载的包中的选项\lstinline结合使用。当然,可以使用 中的适当选项调整用于代码的字体。breaklines=truelistings\lstset

以下 MWE 包含表格的两个不同版本,一个使用tabularx,另一个使用tabular带有p类型列的常规版本。两者的宽度与文本宽度相同。在示例中,我还注释掉了不必要的包,并添加了删除每个包的原因说明。

在此处输入图片描述

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

相关内容