同一行上的多个表格环境中的 \hbox 溢出

同一行上的多个表格环境中的 \hbox 溢出
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=1in]{geometry}

\parindent=0pt
\pagenumbering{gobble}

\begin{document}

  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{E1}
      &
      \textbf{E2}
      &
      \textbf{E3}
      &
      \textbf{E4}
  \\\hline

            O
            O
      &
            O
            O
      &
            O
            X
      &
            O
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{V1}
      &
      \textbf{V2}
      &
      \textbf{V3}
      &
      \textbf{V4}
  \\\hline

            X
            X
      &
            O
            O
      &
            O
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{S1}
      &
      \textbf{S2}
      &
      \textbf{S3}
      &
      \textbf{S4}
  \\\hline

            O
            X
      &
            O
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{A1}
      &
      \textbf{A2}
      &
      \textbf{A3}
      &
      \textbf{A4}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|}\hline

      \textbf{M1}
      &
      \textbf{M2}
      &
      \textbf{M3}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{G1}
      &
      \textbf{G2}
      &
      \textbf{G3}
      &
      \textbf{G4}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}



\end{document}

溢出MWE

我正在编写一个程序,根据成绩册数据自动生成上述 LaTeX 源。具体来说,我不知道程序将生成多少个表格,或者每个表格将有多少列(最多 8 列)。为了节省空间,我想以内联方式生成这些表格,以便它们可以根据需要自动换行。显然,这并不是预期的结果。如果下一个表格溢出水平线宽度,我该如何让行换行,或者有没有更优雅的解决方案来解决这个问题?

答案1

Tex 正在尝试对齐行,但由于表格很大,每行上几乎没有可拉伸的单词空间,因此注定会失败。如果你将块设置得参差不齐,情况就会好转

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=1in]{geometry}

\parindent=0pt
\pagestyle{empty}%\pagenumbering{gobble}

\begin{document}
\raggedright
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{E1}
      &
      \textbf{E2}
      &
      \textbf{E3}
      &
      \textbf{E4}
  \\\hline

            O
            O
      &
            O
            O
      &
            O
            X
      &
            O
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{V1}
      &
      \textbf{V2}
      &
      \textbf{V3}
      &
      \textbf{V4}
  \\\hline

            X
            X
      &
            O
            O
      &
            O
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{S1}
      &
      \textbf{S2}
      &
      \textbf{S3}
      &
      \textbf{S4}
  \\\hline

            O
            X
      &
            O
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{A1}
      &
      \textbf{A2}
      &
      \textbf{A3}
      &
      \textbf{A4}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|}\hline

      \textbf{M1}
      &
      \textbf{M2}
      &
      \textbf{M3}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{G1}
      &
      \textbf{G2}
      &
      \textbf{G3}
      &
      \textbf{G4}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}% best not to have this last space\hspace{2em}



\end{document}

答案2

我将每个出现的 替换\hspace{2em}\hspace{2em}\hfill(注释掉最后的 )\hfill

这将提供对齐边距,相邻表格之间的最小间隙为 2em。表格将均匀分布在给定的行上。

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=1in]{geometry}

\parindent=0pt
\pagenumbering{gobble}

\begin{document}

  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{E1}
      &
      \textbf{E2}
      &
      \textbf{E3}
      &
      \textbf{E4}
  \\\hline

            O
            O
      &
            O
            O
      &
            O
            X
      &
            O
            X
  \\\hline
  \end{tabular}\hspace{2em}\hfill
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{V1}
      &
      \textbf{V2}
      &
      \textbf{V3}
      &
      \textbf{V4}
  \\\hline

            X
            X
      &
            O
            O
      &
            O
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}\hfill
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{S1}
      &
      \textbf{S2}
      &
      \textbf{S3}
      &
      \textbf{S4}
  \\\hline

            O
            X
      &
            O
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}\hfill
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{A1}
      &
      \textbf{A2}
      &
      \textbf{A3}
      &
      \textbf{A4}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}\hfill
  \begin{tabular}[t]{|c|c|c|}\hline

      \textbf{M1}
      &
      \textbf{M2}
      &
      \textbf{M3}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}\hfill
  \begin{tabular}[t]{|c|c|c|c|}\hline

      \textbf{G1}
      &
      \textbf{G2}
      &
      \textbf{G3}
      &
      \textbf{G4}
  \\\hline

            X
            X
      &
            X
            X
      &
            X
            X
      &
            X
            X
  \\\hline
  \end{tabular}\hspace{2em}%\hfill



\end{document}

在此处输入图片描述

相关内容