如何对齐表格以使其不会水平溢出页面

如何对齐表格以使其不会水平溢出页面

我需要这个来适应页面,但它溢出了!我尝试参考链接

  1. 如何自动缩小表格以使其适合页面?
  2. Latex 表格超出页面边框

但没有成功。以下是表格。

\begin{center}
 \begin{tabular}{|1|c|c|c|c|c|} 
 \hline
 ID & Test Name & Test Description & Input & Expected Output & Actual Output \\ [0.5ex]
 \hline
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\ 
 \hline
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
\end{tabular}
\tabcap{Sound Limit in different zones}
\end{center}

答案1

两个解决方案基于tabularx。第二个使用booktabs包,只有 3 条水平规则:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{array, caption, tabularx,  ragged2e,  booktabs}

\begin{document}\

\noindent\setlength\tabcolsep{4pt}%
\begin{tabularx}{\linewidth}{|l|c|*{4}{>{\RaggedRight\arraybackslash}X|}}
  \hline
  ID & Test Name & Test Description           & Input                & Expected Output              & Actual Output   \\ [0.5ex]
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
\end{tabularx}
\captionof{table}{Sound Limit in different zones}
\vskip1cm

\noindent\setlength\tabcolsep{4pt}%
\begin{tabularx}{\linewidth}{lc*{4}{>{\RaggedRight\arraybackslash}X}}
  \toprule
  ID & Test Name & Test Description           & Input                & Expected Output              & Actual Output   \\ [0.5ex]
  \midrule
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \addlinespace
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \addlinespace
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \addlinespace
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \bottomrule
\end{tabularx}
\captionof{table}{Sound Limit in different zones}

\end{document} 

在此处输入图片描述

答案2

除了完全重新制定表格之外,还有几个可能的步骤可以提供帮助,如下所示:

1)您可以减小表格的字体大小

2)您可以通过重新指定长度来减少每列的空白填充\tabcolsep

3)如果允许对称边距超限(在我的组织中是这样的),您可以将表格放入\makebox宽度小于或等于的区域中\textwidth,这将迫使任何边距重叠左右对称分布,而不是超出右边距。

这是 MWE。注意:由于\tabcap未提供宏,我将其改为\captionofcaption

\documentclass{article}
\usepackage{caption,lipsum}
\begin{document}
\begin{center}
 \tabcolsep=1pt\relax
 \small% <<--- CAN USE \footnotesize OR \scriptsize IF NEEDED
\captionof{table}{Sound Limit in different zones}
 \makebox[\textwidth]{\begin{tabular}{|l|c|c|c|c|c|} 
 \hline
 ID & Test Name & Test Description & Input & Expected Output & Actual Output \\ [0.5ex]
 \hline
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\ 
 \hline
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
\end{tabular}}
%\tabcap{Sound Limit in different zones}
\end{center}\medskip
\lipsum[1]
\end{document}

在此处输入图片描述

虽然有办法缩放tabular内容以精确适应文本边距,但我一般不推荐这种方法,因为这意味着每个超大表格都会以自己独特的字体大小呈现。如果文档中有多个这样的表格,那么看到每个表格的不同缩放比例可能会让人感到视觉不适。

答案3

我建议您 (a) 使用tabularx环境 -- 并将宽度设置为\textwidth-- 而不是tabular环境,(b)X对第 3 至第 6 列使用 raggedright 版本的列类型,以及 (c) 减少列间空白量。您可能还想考虑删除所有垂直条并使用更少(但间距适当)的水平条,以使表格看起来更开放。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\begin{document}
\begin{table}
\setlength\tabcolsep{3pt} % default value: 6pt
\caption{Sound Limit in different zones}
\centering
 \begin{tabularx}{\textwidth}{@{} ll *{4}{L} @{}} 
 \toprule
 ID & Test Name & Test Description & Input & Expected Output & Actual Output \\ [0.5ex]
 \midrule
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\ 
 \addlinespace
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \addlinespace
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \addlinespace
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \bottomrule
\end{tabularx}
\end{table}
\end{document}

相关内容