如何在 tikz 中圈出部分表格?

如何在 tikz 中圈出部分表格?

我正在尝试创建一个大致如下图所示的图表:

表格版本

除了我希望它有圆角。似乎最明显的方法是使用 tikz,所以我尝试了以下方法:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{braket}
\usetikzlibrary{matrix,fit}

\begin{document}
\newcommand{\objboxthreer}[7]{
  \begin{tikzpicture}
    \matrix (m) [nodes={inner sep=2pt},matrix of nodes, nodes in empty cells,ampersand replacement=\&, row sep=0pt, column sep=0pt]
    {
      {#1} \& \& {#2} \& {#4} \& {#6} \& \\
           \& \& {#3} \& {#5} \& {#7} \& \\
    };
    \node [
      draw,
      rounded corners=.5em,
      inner sep=0pt,
      outer sep=0pt,
      minimum size=0pt,
      fit=(m-1-2.north east)
          (m-1-2.south east)
          (m-1-3.north east)
          (m-1-3.south east)
          (m-1-4.north east)
          (m-1-4.south east)
          (m-1-5.north east)
          (m-1-5.south east)
          (m-1-6.north west)
          (m-1-6.south west)] {};
    %\draw [rounded corners=.5em] (m-1-2.south east) rectangle (m-3-6.north west);
    \draw (m-1-3.north east) -- (m-1-3.south east);
    \draw (m-1-4.north east) -- (m-1-4.south east);
  \end{tikzpicture}
}
\objboxthreer{d}{$p_s$}{$Y_d$}{$p_d$}{$Y_d'$}{$\{p\}$}{$\Set{ x \in X | x > 5}$}

\end{document}

这产生了不太令人满意的结果:

tikz 版本

垂直线未到达封闭框的顶部或底部。

挑战在于,此表格中所有单元格的内容大小都非常动态。每个元素的高度和宽度都可能发生很大变化(复杂的公式等),因此我无法简单地指定每行和每列的高度和宽度。我如何才能模拟第一个表格但带有圆角?

这里有一个具有更高元素的附加测试用例:\documentclass{standalone} \usepackage{tikz} \usepackage{braket} \usetikzlibrary{matrix,fit}

\begin{document}
\newcommand{\objboxthreer}[7]{
  \begin{tikzpicture}
    \matrix (m) [nodes={inner sep=2pt},matrix of nodes, nodes in empty cells,ampersand replacement=\&, row sep=0pt, column sep=0pt]
    {
      {#1} \& \& {#2} \& {#4} \& {#6} \& \\
           \& \& {#3} \& {#5} \& {#7} \& \\
    };
    \node [
      draw,
      rounded corners=.5em,
      inner sep=0pt,
      outer sep=0pt,
      minimum size=0pt,
      fit=(m-1-2.north east)
          (m-1-2.south east)
          (m-1-3.north east)
          (m-1-3.south east)
          (m-1-4.north east)
          (m-1-4.south east)
          (m-1-5.north east)
          (m-1-5.south east)
          (m-1-6.north west)
          (m-1-6.south west)] {};
    %\draw [rounded corners=.5em] (m-1-2.south east) rectangle (m-3-6.north west);
    \draw (m-1-3.north east) -- (m-1-3.south east);
    \draw (m-1-4.north east) -- (m-1-4.south east);
  \end{tikzpicture}
}
\objboxthreer{d}{$p_s$}{$Y_d$}{$\left\{\frac{p}{q}\right\}$}{$Y_d'$}{$\{p\}$}{$\Set{ x \in X | x > 5}$}

\end{document}

其输出有类似的问题,但无法通过使用以下方法解决\strut在此处输入图片描述

答案1

虽然添加\strut可以解决基本文本的问题,但是如果您想允许更多可变高度,则\vphantom需要添加基于最高条目高度的:

在此处输入图片描述

参考:

代码:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{braket}
\usetikzlibrary{matrix,fit}

\begin{document}
\newcommand{\objboxthreer}[7]{%
    \newcommand{\MyStrut}{\vphantom{#1#2#3#4#5#6#7}}%
  \begin{tikzpicture}
    \matrix (m) [nodes={inner sep=2pt},matrix of nodes, nodes in empty cells,ampersand replacement=\&, row sep=0pt, column sep=0pt]
    {
      {#1\MyStrut} \& \& {#2\MyStrut} \& {#4\MyStrut} \& {#6\MyStrut} \& \\
           \& \& {#3\MyStrut} \& {#5\MyStrut} \& {#7\MyStrut} \& \\
    };
    \node [
      draw,
      rounded corners=.5em,
      inner sep=0pt,
      outer sep=0pt,
      minimum size=0pt,
      fit=(m-1-2.north east)
          (m-1-2.south east)
          (m-1-3.north east)
          (m-1-3.south east)
          (m-1-4.north east)
          (m-1-4.south east)
          (m-1-5.north east)
          (m-1-5.south east)
          (m-1-6.north west)
          (m-1-6.south west)] {};
    %\draw [rounded corners=.5em] (m-1-2.south east) rectangle (m-3-6.north west);
    \draw (m-1-3.north east) -- (m-1-3.south east);
    \draw (m-1-4.north east) -- (m-1-4.south east);
  \end{tikzpicture}%
}
\objboxthreer{d}{$p_s$}{$Y_d$}{$\left\{ \frac{p}{q} \right\}$}{$Y_d'$}{$\{p\}$}{$\Set{ x \in X | x > 5}$}

\end{document}

答案2

用最少的工具解决,没有matrix但有shape multiple parts

 \documentclass{article}
 \usepackage{tikz}\usepackage{braket}
 \usetikzlibrary{shapes.multipart}
 \begin{document}
 \setlength{\fboxsep}{0pt}
 \begin{tikzpicture}[%
     table shape/.style={rectangle split,
                         rectangle split part align=base, 
                         rectangle split horizontal, 
                         rectangle split parts=3,
                         rounded corners},
     p3/.style={text width=18ex,align=center}]

  \node [table shape,draw] (main) 
     {$p_s$ 
     \nodepart{two}$p_d$ 
     \nodepart[p3]{three}$\{p\}$};
  \node [table shape] at ([yshift=-2em]main.center)
     {$Y_d$ 
     \nodepart{two}$Y'_d$ 
     \nodepart[p3]{three}$\Set{ x \in X | x > 5}$};
  \node at ([xshift=-3ex]main.text west) {d};
 \end{tikzpicture}
 \end{document}   

在此处输入图片描述

相关内容