我的数据超出了表格的边缘

我的数据超出了表格的边缘

这是我的代码:

\documentclass{article}
\begin{document}
    \begin{tabular}{|p{4cm}|p{4cm}|}
        \hline Class Name & WriteReviewController
        \\ 
        \hline 
        
        \hline Superclass & -
        \\ 
        \hline 
        
        \hline Subclasses & -
        \\ 
        \hline 
        
        \hline Interfaces & View.OnClickListener \newline RatingBar.OnRatingBarChangeListener \newline SwitchCompat.OnCheckedChangeListener
        \\ 
        \hline
        
        \hline Responsability & Collaborators
        \\ 
        \hline 
        
        \hline Consente di aggiornare il proprio profilo & ProfileFragment \newline AccountDAO \newline DAOFactory \newline ConfigReader
        \\ 
        \hline 
    \end{tabular} 
\end{document}

正如你在图片中看到的那样在此处输入图片描述

数据超出右边距。如何自动缩放表格,以便字符串正确适合框?

答案1

以下可能更接近期望的输出:

在此处输入图片描述

\documentclass{article}
\usepackage{ragged2e}
\usepackage{tabularx}
\begin{document}
    \begin{tabularx}{\textwidth}{|>{\RaggedRight\arraybackslash}X|l|}
        \hline Class Name & WriteReviewController  \\ 
        \hline 
        Superclass & -  \\ 
        \hline 
        Subclasses & -  \\ 
        \hline  
        Interfaces & \begin{tabular}[t]{@{}l@{}}
                       View.OnClickListener \\
                       RatingBar.OnRatingBarChangeListener \\
                       SwitchCompat.OnCheckedChangeListener  
                     \end{tabular}\\ 
        \hline 
        Responsability & Collaborators  \\ 
        \hline 
        Consente di aggiornare il proprio profilo & \begin{tabular}[t]{@{}l@{}}
                                                      ProfileFragment \\
                                                      AccountDAO \\
                                                      DAOFactory \\
                                                      ConfigReader  
                                                    \end{tabular} \\ 
        \hline 
    \end{tabularx} 
\end{document}

答案2

如果必须将两列的宽度都设置为4cm,我建议您使用该seqsplit包及其\seqsplit宏让 LaTeX 在两个长字符串中创建自动换行符。

在此处输入图片描述

\documentclass{article}
\usepackage{seqsplit,array}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{Q}[1]{>{\hangafter=1\hangindent=1em}P{#1}}
\begin{document}
\setlength\extrarowheight{3pt} % for a more open "look"
\noindent
\begin{tabular}{|Q{4cm}|P{4cm}|}
   \hline Class Name & WriteReviewController
   \\ \hline   
   Superclass & --
   \\ \hline
   Subclasses & --
   \\ \hline
   Interfaces & View.OnClickListener \newline
   \seqsplit{RatingBar.OnRatingBarChangeListener} \newline
   \seqsplit{SwitchCompat.OnCheckedChangeListener}
   \\ \hline 
   Responsibility & Collaborators
   \\ \hline   
   Consente di aggiornare il proprio profilo 
   & ProfileFragment \newline 
   AccountDAO \newline 
   DAOFactory \newline 
   ConfigReader
   \\ \hline 
\end{tabular} 
\end{document}

相关内容