如何获取表格行上的符号

如何获取表格行上的符号

我想用 latex 生成这样的表格。你能帮忙吗?我可以像下面这样写一个普通的表格,但不知道如何在行上创建这些符号。

\begin{table}[htbp]
\centering
\caption{Add caption}
\begin{tabular}{|l|l|l|}
\toprule
      & Scenario 1 & Scenario 2 \\
\midrule
Val 1 & x     & y \\
\midrule
Val 2 & x     & Z \\
\bottomrule
\end{tabular}%
\label{tab:addlabel}%
\end{table}%

线上有符号的表格

答案1

这是一个使用的解决方案tikz

\documentclass[tikz, border = 5pt]{standalone}

\usetikzlibrary{matrix}
\usetikzlibrary{calc}

\tikzset{
  table/.style={
    matrix of nodes,
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    nodes={
      rectangle,
      draw,
      text width=6em,
      align=center
    },
    minimum height=1.2cm,
    text depth=0.5ex,
    text height=2ex,
    nodes in empty cells,
    row 1/.style={
      minimum height=0.5cm,
    },
    column 1/.style={
      nodes={
        text width=3em
      }
    }
    },
  circ/.style={
    draw=green,
    circle,
    fill=white,
    thick,
    minimum width=0.8cm
  }
  }


\begin{document}
\begin{tikzpicture}
  \matrix (table) [ampersand replacement=\&, table]
  {
   \& Scenario 1 \& Scenario 2 \\
   Val 1 \& $\alpha + \beta^2$ \& $\gamma + \beta\delta^2$ \\
   Val 2 \& $\alpha + \beta^2$ \& $\alpha + \beta^2$ \\
  };

  \node[circ] (less) at ($(table-2-2)!0.5!(table-2-3)$){$<$};
  \node[circ] (equals) at ($(table-2-2)!0.5!(table-3-2)$){$=$};
  \node[circ] (question) at ($(table-3-2)!0.5!(table-3-3)$){$?$};
  \node[circ, rotate = -90] (greater) at ($(table-2-3)!0.5!(table-3-3)$){$>$};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

解决方案pstricks。请注意,您不应将 中的规则booktabs与垂直规则一起使用,因为它们不相交。我将它们替换为\Xhline中的 s makecell,其中必须指定厚度:

\documentclass[svgnames]{article}
\usepackage{booktabs}
\usepackage{makecell, caption}
\usepackage{pst-node}

\begin{document}

  \begin{table}[htbp]
\centering
\setlength{\tabcolsep}{8pt}
\setcellgapes{9pt}\makegapedcells
\caption{Add caption}
\begin{tabular}{!{\vline width 1pt}l|l|l!{\vline width 1pt}}
\Xhline{1pt}
      & Scenario 1 & Scenario 2 \\
\Xhline{0.5pt}
Val 1 & \Rnode{S1t}{$\alpha + \beta^2 $} & \Rnode{S2t}{~$\gamma + \beta\delta^2 $} \\
\Xhline{0.5pt}
Val 2 & \Rnode{S1b}{$\alpha + \beta^2 $ }& \Rnode{S2b}{~$\alpha + \beta^2 $} \\
\Xhline{1pt}
\end{tabular}%
\label{tab:addlabel}%
\psset{linecolor=YellowGreen, fillstyle=solid, fillcolor=White,}
\nput[labelsep=1.5em]{0}{S1t}{\pscirclebox{$ < $}}
\nput[labelsep=1.25em]{0}{S1b}{\pscirclebox{?}}
\nput[labelsep=0.1ex]{105}{S1b}{\pscirclebox{$ = $}}
\nput[labelsep=0.05ex, rot=-90]{90}{S2b}{\pscirclebox{$ > $}}
\end{table}%

\end{document}

在此处输入图片描述

答案3

这个答案试图结合现有的好答案的优点作者 Bernard来自 caverac。我喜欢 Bernard 的回答,因为你可以使用普通的 LaTeX 表格及其所有强大的方法来对齐事物等等。(但请注意,我将右下角的条目居中,就像您的屏幕截图一样。)在我看来,caverac 的回答最大的优点是,你不受编译器选择的限制,因为它基于 TiZ,并且没有硬编码的维度。(我使用midway而不是语法calc,不是因为我认为它“更好”,而只是为了展示一种替代方案。)为此,我使用该tikzmark库来得到可以称为 TiBernard 提案的 Zy 版本(大部分代码均借鉴自该版本)

\documentclass[svgnames]{article}
\usepackage{booktabs}
\usepackage{makecell, caption}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

  \begin{table}[htbp]
\centering
\setlength{\tabcolsep}{8pt}
\setcellgapes{9pt}\makegapedcells
\caption{Add caption}
\begin{tabular}{!{\vline width 1pt}l|c|c!{\vline width 1pt}}
\Xhline{1pt}
      & \multicolumn{1}{l|}{Scenario 1} &\multicolumn{1}{l!{\vline width 1pt}}{Scenario 2} \\
\Xhline{0.5pt}
Val 1 & \tikzmarknode{S1t}{$\alpha + \beta^2 $} & \tikzmarknode{S2t}{$\gamma +
\beta\,\delta^2 $} \\
\Xhline{0.5pt}
Val 2 & \tikzmarknode{S1b}{$\alpha + \beta^2 $ }& \tikzmarknode{S2b}{$\alpha + \beta^2 $} \\
\Xhline{1pt}
\end{tabular}%
\label{tab:addlabel}%
\begin{tikzpicture}[overlay,remember
picture,gc/.style={midway,circle,draw=green!60!black,thick,fill=white,inner
sep=2pt}]
\path (S1t.east) -- (S2t.west) node[gc] {$<$};
\path (S1b.east) -- (S2b.west) node[gc] {?};
\path (S1t.south) -- (S1b.north) node[gc] {$=$};
\path (S2t.south) -- (S2b.north) node[gc,rotate=90] {$<$};
\end{tikzpicture}%
\end{table}%
\end{document}

在此处输入图片描述

相关内容