如何在 Tikz 中整理/调整我的表格

如何在 Tikz 中整理/调整我的表格

我想把我的这个表格整理一下,第二行的概率摆放的很乱,第一行的空隙也太多了。

这是表格代码:

\documentclass{article}\usepackage{array}  
\begin{document}
    \centering
\begin{table}[htbp] \setlength\extrarowheight{2pt}  
    \[      
    \begin{array}{c|cccc} 
        \hline x & 0 & 1 & 2 & 3 \\              
        \hline P(X=x) & \frac{1}{8} & \frac{3}{8} & \frac{3}{8} & \frac{1}{8} 
    \end{array}  
    \]     
    \caption{The probability distribution of $X$, a listing of all possible values of $X$, together with their corresponding probabilities.}
\end{table}  

\end{document}

谢谢。

答案1

您的表格代码如果充满了杂乱的\multicolumns 和\mulotirowsBoth 都是多余的......

两种可能的解决方案:

  • 附表array
\documentclass{article}
\usepackage{array}

\begin{document}
\[  \setlength\extrarowheight{2pt}
\begin{array}{c|cccc}
\hline x        & 0 & 1 & 2 & 3 \\
\hline P(X=x)   & \frac{1}{8}
                    & \frac{3}{8}
                        & \frac{3}{8}
                            & \frac{1}{8}
\end{array}
\]
\end{document}
  • 附带tblrtabularray包装:
\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{table}
\centering
    \begin{tblr}{colspec = { Q[c,$] | *{4}{Q[c, $]} }}
    \hline
x         &  0  & 1 & 2 & 3 \\
    \hline
P(X=x)    &  \frac{1}{8}
                & \frac{3}{8}
                    & \frac{3}{8}
                        & \frac{1}{8}
\end{tblr}
    \end{table}
\end{document}

两者的结果相同:

在此处输入图片描述

答案2

有很多方法可以解释和解决您的问题。我是这样理解的:

  • 使代码看起来更清晰,更容易理解
  • 使打印件读起来更加赏心悦目。

以下是实现这一目标的一些策略。

1. 让代码看起来更简洁

秘诀就是像布局人员一样充分利用空白。缩进是此策略的一部分。也许您从结构化编程中了解它。但是,我想称之为defensive programming,使结构上的障碍几乎不言而喻。因此,在第一次重新排列后,您的代码可能看起来像这样:

\documentclass{article}
\usepackage{array}%     it's easier to grasp this way on the fly  


\begin{document}
  % ~~~ indenting, in the tradition of strucutred programming ~~
  \begin{table}[htbp]
    \centering%     you probably only want the table centered
    \setlength\extrarowheight{2pt}
    % ~~~ reflect intended structure in code itself ~
    \[      
    \begin{array}{c|cccc} 
        \hline
         x & 0 & 1 & 2 & 3 \\              
        \hline
        P(X=x) & \frac{1}{8} & \frac{3}{8} 
               & \frac{3}{8} & \frac{1}{8} 
    \end{array}  
    \]     
    \caption{The probability distribution of $X$, a listing of all possible values of $X$, together with their corresponding probabilities.}
  \end{table} 
  
\end{document}

结果-1

在我看来,有点让人分心的是:长标题。因此,让我们将其导出到宏中\cptn,这将清理下面的表格代码:

\documentclass{article}
\usepackage{array}%     it's easier to grasp this way on the fly  

% ~~~ some shorthand; hatch the needed % (!) inside macros ~~~
\newcommand\cptn[0]{The probability distribution of $X$, a%
                    listing of all possible values of $X$,%
                    together with their corresponding% 
                    probabilities.}
                    
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
  % ~~~ indenting, in the tradition of strucutred programming ~~
  \begin{table}[htbp]
    \centering%     you probably only want the table centered
    \setlength\extrarowheight{2pt}
    % ~~~ reflect intended structure in code itself ~
    \[      
    \begin{array}{c|cccc} 
        \hline
         x & 0 & 1 & 2 & 3 \\              
        \hline
        P(X=x) & \frac{1}{8} & \frac{3}{8} 
               & \frac{3}{8} & \frac{1}{8} 
    \end{array}  
    \]     
    \caption{\cptn}
  \end{table} 
  
\end{document}

结果-2

2. 为书中的表格进行排版

书籍排版人员的良好做法是删除几乎所有的表格线、背景和阴影。此包booktabs就是为此而制作的。

因此,我们需要得到一张表,但目前我做不到,因为您使用了包array。因此,让我们将其转换为表结构。

经过一些“代码修改”之后,还剩下一些工作要做,这就是结果:

  • 取而代之arraytabular
  • 引入宏\mathline,在数学模式中有一行
  • 缩写重复的\frac术语。

一会儿效果就会好起来。

\documentclass{article}

% ~~~ some shorthand; watch the needed % (!) inside macros ~~~
\newcommand\cptn[0]{The probability distribution of $X$, a%
                    listing of all possible values of $X$,%
                    together with their corresponding% 
                    probabilities.}

% ~~~ one table line in math-mode ~~~~~~~~~~~~~
\newcommand\mathline[5]{$#1$ & $#2$ & $#3$ & $#4$ & $#5$}

% ~~~ let's abbreviate those repeating fractions ~~~~~~~
\newcommand\fr[0]{\frac{1}{8}}
\newcommand\frk[0]{\frac{3}{8}}
                    
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
  % ~~~ indenting, in the tradition of strucutred programming ~~
  \begin{table}[htbp]
    \centering%     you probably only want the table centered

    % ~~~ reflect intended structure in code itself ~
    \begin{tabular}{c|cccc} 
        \hline          
        \mathline{x}{0}{1}{2}{3} \\              
        \hline
        \mathline{P(X=x)}{\fr}{\frk}{\frk}{\fr}
    \end{tabular}  
    \caption{\cptn}
  \end{table} 
  
\end{document}

结果-3

最后让我们开始吧booktabs。此包提供:

  • \toprule表格\bottomrule两端的线条略粗
  • \midrule对于表格内的任何水平线(像稀有香料一样使用)
  • 此外,这个包还引入了一些“空气”(白色空间),使得书中的表格看起来更美观。

按照没有垂直规则的布局思想,列说明符变得简单{ccccc}

\documentclass{article}
\usepackage{booktabs}

% ~~~ some shorthand; watch the needed % (!) inside macros ~~~
\newcommand\cptn[0]{The probability distribution of $X$, a%
                    listing of all possible values of $X$,%
                    together with their corresponding% 
                    probabilities.}

% ~~~ one table line in math-mode ~~~~~~~~~~~~~
\newcommand\mathline[5]{$#1$ & $#2$ & $#3$ & $#4$ & $#5$}

% ~~~ let's abbreviate those repeating fractions ~~~~~~~
\newcommand\fr[0]{\frac{1}{8}}
\newcommand\frk[0]{\frac{3}{8}}
                    
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
  % ~~~ indenting, in the tradition of strucutred programming ~~
  \begin{table}[htbp]
    \centering%     you probably only want the table centered

    % ~~~ reflect intended structure in code itself ~
    \begin{tabular}{ccccc} 
        \toprule         
        \mathline{x}{0}{1}{2}{3} \\              
        \midrule
        \mathline{P(X=x)}{\fr}{\frk}{\frk}{\fr}\\
        \bottomrule
    \end{tabular}  
    \caption{\cptn}
  \end{table} 
  
\end{document}

现在你看到了:

结果-4

相关内容