表の列が収まらない

Aug 21 2020

私は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}

回答

4 Zarko Aug 21 2020 at 12:55

逐語的なテキストは長すぎて、表に収まりません。代わりにフォント\verbを使用\ttfamilyして有効にし、2列目のテキストをより多くの行に分割するという解決策かもしれません。

\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

ドキュメントのコンパイル時に表示される警告を詳しく見ると、次のことがわかります。

\verb may be unreliable inside tabularx

これは、を使用しtabularxたにもかかわらず、テーブルがまだマージンにオーバーフローしている理由についてのヒントをすでに示しています。これを克服するために、プリアンブルにすでにロードされているパッケージ\lstinlinebreaklines=trueオプションと組み合わせて使用できlistingsます。もちろん、コードに使用されるフォントは、内の適切なオプションを使用して調整できます\lstset

次のMWEには、テーブルの2つの異なるバージョンが含まれています。1つはを使用しtabularx、もう1つtabularpタイプ列を持つ通常のバージョンです。どちらもテキスト幅と同じ幅です。この例では、不要なパッケージもコメントアウトし、各パッケージを削除した理由の説明を追加しました。

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