表格超出右边距

表格超出右边距

我是 LaTeX 的新手,我正在尝试将我的表格转换为 Excel2Latex。

这是我的乳胶文档:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{multirow}
\usepackage{float} 
\usepackage{tabularx}

\begin{document}


    \begin{table}[htbp]
        \centering
        \caption{Add caption}
        \begin{tabularx}{\textwidth}{lrrr}
            \hline
            Use Case \# & \multicolumn{1}{l}{Name} &       &  \\ \hline
            Goal in Contect & \multicolumn{1}{l}{Obiettivo} &       &  \\ \hline
            Preconditions & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Success End Conditions & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Failed End Conditions & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Primary Actor & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Trigger & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Descriptions & \multicolumn{1}{l}{Step} & \multicolumn{1}{l}{User Acton} & \multicolumn{1}{l}{System} \\ \hline
            & \multicolumn{1}{l}{\#1} & \multicolumn{1}{p{25.355em}}{Lorem ipsum dolor sit amet, consectetur adipiscing elit.\newline{}Mauris posuere nisl et risus viverra sagittis ut et est. Morbi nec nunc imperdiet, pulvinar quam sed, lobortis arcu. Nulla sagittis tortor ac eros tincidunt lacinia. Cras ut venenatis ante, eu condimentum arcu. Ut luctus rutrum fermentum. Mauris aliquam magna ex, vel.} &  \\ \hline
            Extensions & \multicolumn{1}{l}{Step} & \multicolumn{1}{l}{User Action} & \multicolumn{1}{l}{System} \\ \hline
            &       &       &  \\ \hline
            Subvariations & \multicolumn{1}{l}{Step} & \multicolumn{1}{l}{User Action} & \multicolumn{1}{l}{System} \\ \hline
            &       &       &  \\ \hline
            Notes &       &       &  \\ \hline
        \end{tabularx}%
        \label{tab:addlabel}%
    \end{table}%

\end{document}

但不幸的是,正如你所见,它超出了页面的右边距这里

  • 我该如何修复?

答案1

  • 如果使用tabularx至少一个列类型应该是X类型(或其衍生物)
  • \multicolumn{1}{p{25.355em}{ ... long text ...}太宽。删除此命令并为此列使用X列类型
  • 删除所有\mullticolumn{1}{l}{...}。它们只是杂乱无章。而是相应地设置列类型(从rl
  • 我会删除大部分水平线
  • 对于主要的,我将使用booktabs包中定义的规则
\documentclass[a4paper]{article}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, tabularx}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[htbp]
\caption{Add caption}
\label{tab:addlabel}
    \setcellgapes{2pt}
    \makegapedcells
\begin{tabularx}{\textwidth}{@{} l l L r @{}}
    \toprule
Use Case \#     & Name      &       &           \\ 
    \midrule
Goal in Contect & Obiettivo &       &           \\ 
%    \hline
Preconditions   & ok        &       &           \\ 
%    \hline
Success End Conditions 
                & ok        &       &           \\ 
%    \hline
Failed End Conditions 
                & ok        &       &           \\ 
%    \hline
Primary Actor   & ok        &       &           \\ 
%   \hline
Trigger         & ok        &       &           \\
    \hline
Descriptions    & Step      & User Action    
                                    & System    \\ 
%    \hline
                & \#1       & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris posuere nisl et risus viverra sagittis ut et est. Morbi nec nunc imperdiet, pulvinar quam sed, lobortis arcu. Nulla sagittis tortor ac eros tincidunt lacinia. Cras ut venenatis ante, eu condimentum arcu. Ut luctus rutrum fermentum. Mauris aliquam magna ex, vel.
                                    &           \\
    \hline
Extensions      & Step      & User Action
                                    & System    \\ 
%    \hline
                &           &       &           \\ 
    \hline
Subvariations   & Step      & User Action
                                    & System    \\ 
                &           &       &           \\ 
    \hline
Notes           &           &       &           \\ 
    \bottomrule
\end{tabularx}%
    \end{table}%
\end{document}

在此处输入图片描述

(红线表示文本边框)

附录: 考虑到@Barbara Beeton 的评论,上述 MWE 可以重新设计如下:

\documentclass[a4paper]{article}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}
\newlength{\pwidth}

\begin{document}
    \begin{table}[htbp]
\caption{Add caption}
\label{tab:addlabel}
    \settowidth{\pwidth}{Preconditions}
\begin{tabularx}{\textwidth}{@{} >{\raggedright}m{\pwidth} % <--- changed
                                 l L r @{}}
    \toprule
Use Case \#     & Name      &       &           \\
    \midrule
Goal in Contect & Obiettivo &       &           \\
    \addlinespace    % <--- added, instead of \hline
Preconditions   & ok        &       &           \\
    \addlinespace
Success End Conditions
                & ok        &       &           \\
    \addlinespace
Failed End Conditions
                & ok        &       &           \\
    \addlinespace
Primary Actor   & ok        &       &           \\
   \addlinespace
Trigger         & ok        &       &           \\
    \midrule
Descriptions    & Step      & User Action
                                    & System    \\
    \addlinespace
                & \#1       & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris posuere nisl et risus viverra sagittis ut et est. Morbi nec nunc imperdiet, pulvinar quam sed, lobortis arcu. Nulla sagittis tortor ac eros tincidunt lacinia. Cras ut venenatis ante, eu condimentum arcu. Ut luctus rutrum fermentum. Mauris aliquam magna ex, vel.
                                    &           \\
    \midrule
Extensions      & Step      & User Action
                                    & System    \\
    \addlinespace
                &           &       &           \\
    \midrule
Subvariations   & Step      & User Action
                                    & System    \\
                &           &       &           \\
    \midrule
Notes           &           &       &           \\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

相关内容