格式化具有相等列宽的表格时出现的问题

格式化具有相等列宽的表格时出现的问题

我想创建两个具有相同列宽的表格。两个表格都由 6 行和 3 列组成。在这两个表格中,第 1 行和第 4 行都是说明性的,并且应该在所有列中展开。在第一个表格中,第 2、3、5 和 6 行应该有 3 列相同的宽度。在第二个表格中,第 2 行和第 3 行应该有 2 列相同的宽度,第 5 行和第 6 行应该有 3 列相同的宽度。

这就是我制作第一个表格的方法:

    \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\pi$.}
        \label{tab:distPi} \centering
        \begin{tabular*}{0.45\textwidth}{@{\extracolsep{\fill}}|c|c|c|}
            \hline
                \multicolumn{3}{|c|}{\textbf{Name of the SLA parameter $\pi$}} \\
            \hline
                \emph{Cost} & \emph{Charge} & \emph{Rate} \\
            \hline
                15\% & 15\% & 40\% \\
            \hline
            \hline
                \multicolumn{3}{|c|}{\textbf{Unit of the SLA parameter $\pi$}} \\
            \hline
                \emph{USD} & \emph{GBP} & \emph{YPI} \\
            \hline
                38\% & 2\% & 40\% \\
            \hline
        \end{tabular*}
        \vspace{-0.3cm}
    \end{table}

这就是我制作第二张表的方法:

    \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\mu$.}
        \label{tab:distMu} \centering
        \begin{tabular*}{0.45\textwidth}{@{\extracolsep{\fill}}|c|c|c|c|c|c|}
            \hline
                \multicolumn{6}{|c|}{\textbf{Name of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{3}{|c|}{\emph{MemoryConsumption}} & \multicolumn{3}{|c|}{\emph{Consumption}} \\
            \hline
                \multicolumn{3}{|c|}{70\%} & \multicolumn{3}{|c|}{30\%} \\
            \hline
            \hline
                \multicolumn{6}{|c|}{\textbf{Unit of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{2}{|c|}{\emph{Mbit}} & \multicolumn{2}{|c|}{\emph{Gbit}} & \multicolumn{2}{|c|}{\emph{Tbit}} \\
            \hline
                \multicolumn{2}{|c|}{5\%} & \multicolumn{2}{|c|}{55\%} & \multicolumn{2}{|c|}{40\%} \\
            \hline
        \end{tabular*}
        \vspace{-0.3cm}
    \end{table}

但是,格式并不好,如您在给定的屏幕截图中所看到的。

替代文本

有人知道怎么做更好吗?页面是双列的,表格将位于其中一列。它们的宽度应为列宽的约 90%。

答案1

我相信有很多方法可以解决这一问题。以下简短介绍其神奇之处,\halign希望如果在 LaTeX 中可用,可能会有所帮助。

I don't personally fancy "boxing" up the contents in a table.

tl;dr:

{
  \def\trule{\noalign{\medskip\hrule\medskip}}
  \def\header#1#2{\multispan#1\bf#2\hfill\cr\trule}
  $$
    \vbox{\hrule height1pt\medskip\halign to.4\hsize{
      #\hfil\tabskip.2\hsize plus.1\hsize minus.15\hsize&&#\hfil\cr
      % double ampersands begin the repeating part of the preamble.
      \header{3}{Name of the SLA parameter $\pi$}
      \it Cost&\it Charge&\it Rate\cr
      15\%&5\%&40\%\cr
      \noalign{\bigskip}
      \header{3}{Unit of the SLA parameter $\pi$}
      \it USD&\it GBP&\it YPI\cr
      38\%&2\%&40\%\cr}
    \medskip\hrule height1pt}
  $$
}
% end tl;dr
%
% General remarks about horizontal alignment ("halign" or "table" for short):
% "&" is used to separate columns.
% "\cr" is an abbreviation for "Carriage Return". It ends a row.
% "#" is used in the preamble to tell where the given content goes. Compare
% with positional parameters in, eg. `\def\foo#1{<...>#1<...>}`.
%
\vsize=.7\vsize
\input eplain
% eplain is input purely for the following command:
\doublecolumns
% Some text to show the default vertical spacing.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar
tristique felis, a mollis ante tincidunt eu. Maecenas orci quam, auctor vitae
auctor quis, rhoncus quis nisi. Curabitur tincidunt consectetur turpis, vel
eleifend neque tristique faucibus. Praesent tristique vehicula nisl sed
pharetra.
\bigskip
{% Begin a grouping scope, so that all the settings we define here are confined.
\offinterlineskip% Omit whitespace between rows. Without this setting, there are
                 % gaps between vertical rules.
\tabskip0pt% Tabskip is a glue which will be put in every column.
           % For the beginning of the first column, we don't want any
           % horizontal space, so we set the tabskip to 0pt (it already is by
           % default, but lets be sure about it).
\everycr={\noalign{\hrule}}% For every "Carriage Return", insert a horizontal rule.
\halign to .9\hsize{% Begin the preamble of the table. A preamble defines how the
                    % columns of a table are formatted.
  \strut#&% A strut is a invisible "thing", whose height is the height of a
          % paren. We insert a strut as the first column because we have set
          % the whitespace between rows to be zero for the vertical rules, so
          % we need to make a little vertical room for the actual content.
  \vrule#\tabskip1em plus 2em minus .5em&% We set the glue between the columns so
    % that there is stretchability for the columns.
  \hfil#\hfil&% First (actual content) column is defined to be centered.
  \vrule#&% After the content column, there is a vertical rule.
  \hfil#\hfil&\vrule#&\hfil#\hfil&% Ditto for the other two content columns.
  \vrule#\tabskip0pt% For the last vertical rule -column, we set the
                    % stretchability back to 0pt, so that it doesn't "eat up"
                    % the stretchability of the content columns.
  \cr% End the preamble of the table with a "Carriage Return".
  % Now starts the actual contents of the table:
  &&% Skip the strut-column and the vrule-column.
  \multispan5% Let this column span five columns (content-columns and their
             % accompanied vrule-columns, excluding the last vrule-column)
  \hidewidth% Skip this columns width in the calculations of the column widths,
            % because this is a "header" -column/-row.
  \hfil\bf Name of the SLA parameter $\pi$\hfil% We again center the contents.
  \hidewidth% We need to hide the width of this column on its right-hand-side
            % as well.
  &\cr% Finally we are done with the first header-row. The seemingly surplus
      % column-separation ampersand is there to account for the last
      % vrule-column.
  &&\it Cost&&\it Charge&&\it Rate&\cr% The double ampersands account for the
                                      % vrule-columns.
  &&15\%&&5\%&&40\%&\cr
  \noalign{\smallskip\hrule}% Make a small gap before the next header-row.
  &&\multispan5\hidewidth\hfil\bf Unit of the SLA parameter $\pi$\hfil\hidewidth&\cr
  &&\it USD&&\it GBP&&\it YPI&\cr
  &&38\%&&2\%&&40\%&\cr
}% End of the halign.
}% End of the grouping scope. After the scope, TeX no longer knows what `\cc`
 % or `\header` means. Also the tabskip and interlinespacing are returned.
\bigskip
%
% Now that we're familiar with the basics (and some of the not-so-basics) of
% halign, we could further simplify the recurring commands, as well as separate
% the used dimens for re-usability.
%
\def\mytablehelper{
% First off, the dimens(ions):
\dimen0=.9\hsize% We'll use this dimen to set the width of the table.
\skip0=1em plus2em minus.5em% We'll use this glue to set the tabskip amount.
\offinterlineskip
\tabskip0pt
\everycr={\noalign{\hrule}}
\smallskipamount=2pt% remove stretchability
}
%
% For the recurring commands, there are at least the centered content-columns:
\def\cc#1{\hfil#1\hfil}
% and the header-rows:
\def\header#1#2{\multispan#1\hidewidth\cc{\bf#2}\hidewidth}
% and we might as well throw in the header separators in the mix:
\def\headsep{\noalign{\smallskip\hrule}}
%
% As we're getting greedier and greedier, let's wrap the whole thing in a `\def`:
\def\mytablethreecols#1{\centerline{\vbox{\mytablehelper
  \halign to\dimen0{\strut##&\vrule##\tabskip=\skip0&
  \cc{##}&\vrule##&\cc{##}&\vrule##&\cc{##}&\vrule##\tabskip=0pt\cr
  #1}}}}
% Looking ahead, we're going to need one with just two cols as well. I know. Ugly.
\def\mytabletwocols#1{\centerline{\vbox{\mytablehelper
  \halign to\dimen0{\strut##&\vrule##\tabskip=\skip0&
  \cc{##}&\vrule##&\cc{##}&\vrule##\tabskip=0pt\cr
  #1}}}}
%
% And here we go:
\mytablethreecols{
  &&\header{5}{Name of the SLA parameter $\pi$}&\cr
  &&\it Cost&&\it Charge&&\it Rate&\cr
  &&15\%&&5\%&&40\%&\cr
  \headsep
  &&\header{5}{Unit of the SLA parameter $\pi$}&\cr
  &&\it USD&&\it GBP&&\it YPI&\cr
  &&38\%&&2\%&&40\%&\cr
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar
tristique felis, a mollis ante tincidunt eu. Maecenas orci quam, auctor vitae
auctor quis, rhoncus quis nisi. Curabitur tincidunt consectetur turpis, vel
eleifend neque tristique faucibus. Praesent tristique vehicula nisl sed
pharetra.
\smallskip
\mytabletwocols{
  &&\header{3}{Name of the SLA parameter $\mu$}&\cr
  &&\hidewidth\it MemoryConsumption\hidewidth
  &&\hidewidth\it Consumption\hidewidth&\cr
  &&70\%&&30\%&\cr
}% Ewww, two *different* tables!
\vskip1pt
\mytablethreecols{
  &&\header{5}{Unit of the SLA parameter $\mu$}&\cr
  &&\it Mbit&&\it Gbit&&\it Tbit&\cr
  &&5\%&&55\%&&40\%&\cr
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar
tristique felis, a mollis ante tincidunt eu. Maecenas orci quam, auctor vitae
auctor quis, rhoncus quis nisi. Curabitur tincidunt consectetur turpis, vel
eleifend neque tristique faucibus.

\bye

答案2

这是我的丑陋解决方案....我设法使格式更好...虽然我不太喜欢实现细节。

\documentclass[10pt,letterpaper]{article}
\usepackage{array}

\newlength{\onesixth}
\setlength{\onesixth}{.1667 \linewidth}
\setlength{\onesixth}{.5 \onesixth} 

\newcolumntype{C}{>{\begin{minipage}{2\onesixth}\begin{center}}{c}<{\end{center}\end{minipage}}}
\newcolumntype{D}{>{\begin{minipage}{3\onesixth}\begin{center}}{c}<{\end{center}\end{minipage}}}
\newcolumntype{E}{@{}l@{}} %I don't understand why this is needed...but without it the code doesn't compile

\begin{document}

 \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\pi$.}
        \label{tab:distPi}\centering
        \begin{tabular}{|C|C|C|E} 
            \hline
                \multicolumn{3}{|c|}{\textbf{Name of the SLA parameter $\pi$}} 
                \\\hline 
                  \emph{Cost} & \emph{Charge} &\emph{Rate}&
            \\\hline
                1 & 15 & 40 &\\
            \hline
            \hline
                \multicolumn{3}{|c|}{\textbf{Unit of the SLA parameter $\pi$}} \\
            \hline
                \emph{USD} & \emph{GBP} & \emph{YPI}& \\
            \hline
                38\% & 2\% & 40\% &\\
            \hline
        \end{tabular}
        \vspace{-0.3cm}
    \end{table}
    \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\mu$.}
        \label{tab:distMu} \centering
        \begin{tabular}{|c|c|c|c|c|c|}
            \hline
                \multicolumn{6}{|c|}{\textbf{Name of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{3}{|D|}{\emph{MemoryConsumption}} & \multicolumn{3}{D|}{\emph{Consumption}} \\
            \hline
                \multicolumn{3}{|D|}{70\%} & \multicolumn{3}{D|}{30\%} \\
            \hline
            \hline
                \multicolumn{6}{|c|}{\textbf{Unit of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{2}{|C|}{\emph{Mbit}} & \multicolumn{2}{C|}{\emph{Gbit}} & \multicolumn{2}{C|}{\emph{Tbit}} \\
            \hline
                \multicolumn{2}{|C|}{5\%} & \multicolumn{2}{C|}{55\%} & \multicolumn{2}{C|}{40\%} \\
            \hline
        \end{tabular}
        \vspace{-0.3cm}
    \end{table}

        \end{document}

结果是

替代文本

我很不满意:无法使用\multicolumn{6}{|C|C|C|}{..}and 来让这个烦人的代码E看起来是必需的,这样结束行就不会进入\begin{}...\end{}块中。如果有人知道如何删除这两个代码,请告诉我,或者发布一个更好的解决方案。

哦,顺便说一句,MemoryConsumption 对于我定义的盒子来说太大了...我不确定它是否适合你,但你可以自己摆弄它。

相关内容