在 Tikz Matrix 中使用 Tikzmarknode 是否可行?

在 Tikz Matrix 中使用 Tikzmarknode 是否可行?

我无法在下面的矩阵示例中使用 tikzmarknodes。我已添加要绘制的箭头图像和 MWE

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark,matrix,calc}

\begin{document}
\begin{tikzpicture}[declare function={f(\x)=1/ln(2)*ln(\x);}]
\matrix[matrix of math nodes,nodes={align=center,inner sep=3pt,
   text height=1.5ex,text depth=.25ex,draw=gray!40,ultra thin},draw,inner 
   sep=0pt,ampersand replacement=\&] (mat1)
    at (-5,0){
   |[fill=green!40!gray,text width=15mm]| x,\ \text{or}\ 2^{y}  \& | 
   [fill=green!40!gray,text width=11mm]| y \\ 
                 |[text width=15mm]|   1                      \& |[text 
      width=11mm]|  
     {\pgfmathparse{f(1)}\pgfmathprintnumber{\pgfmathresult}}\\
                                     |[text width=15mm]|   2                       
     \& |[text width=11mm]|  
     {\pgfmathparse{f(2)}\pgfmathprintnumber{\pgfmathresult}}\\
                                     |[text width=15mm]|   4                      
      \& |[text width=11mm]|  
     {\pgfmathparse{f(4)}\pgfmathprintnumber{\pgfmathresult}}\\
                                     |[text width=15mm]|   8                      
      \& |[text width=11mm]|  
     {\pgfmathparse{f(8)}\pgfmathprintnumber{\pgfmathresult}}\\
                                     |[text width=15mm]|   \frac{1}{2}                      
      \& |[text width=11mm]|  
     {\pgfmathparse{f(.5)}\pgfmathprintnumber{\pgfmathresult}}\\
                                     |[text width=15mm]|   \frac{1}{4}                      
      \& |[text width=11mm]|  
     {\pgfmathparse{f(.25)}\pgfmathprintnumber{\pgfmathresult}}\\
                                    };
      \node at ([yshift=-.8cm,xshift=2cm]mat1.south) 
            [circle,draw,font=\small,inner sep=1pt] (1){$1$};
            \node at (1) [right=5pt,text=red] {Select $y$};
      \node at ([yshift=-1.5cm,xshift=.7cm]mat1.south) 
            [circle,draw,font=\small,inner sep=1pt] (2){$2$};                                       
            \node at (2) [right=5pt,text=red] {Compute $x$};
 \end{tikzpicture}
 \end{document}

输出:

在此处输入图片描述

我想要绘制的箭头:

在此处输入图片描述

答案1

你不需要tikzmark这里,因为需要 tikzmark 来将非节点变成节点,但您在这里玩的一切都是在节点。编辑:修复了箭头的方向(非常感谢 manoooh)并借用column 1/.append style={nodes={text width=15mm}},column 2/.append style={nodes={text width=9mm}}扎科斯的回答. (但是,我不同意\pgfmathparse{f(1)}\pgfmathprintnumber{\pgfmathresult}用 来替换\pgfmathtruncatemacro,因为一旦得到非整数结果,这种方法就不起作用。)

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=1/ln(2)*ln(\x);}]
\matrix[matrix of math nodes,nodes={align=center,inner sep=3pt,
   text height=1.5ex,text depth=.25ex,draw=gray!40,ultra thin},
   draw,inner sep=0pt,ampersand replacement=\&,
   column 1/.append style={nodes={text width=15mm}},  
   column 2/.append style={nodes={text width=9mm}} ] (mat1)
    at (-5,0){
   |[fill=green!40!gray]| x~\text{or}~2^{y}  \& | 
   [fill=green!40!gray]| y \\ 
     1  \&  {\pgfmathparse{f(1)}\pgfmathprintnumber{\pgfmathresult}}\\
     2  \&  {\pgfmathparse{f(2)}\pgfmathprintnumber{\pgfmathresult}}\\
     4  \&  {\pgfmathparse{f(4)}\pgfmathprintnumber{\pgfmathresult}}\\
     8  \&  {\pgfmathparse{f(8)}\pgfmathprintnumber{\pgfmathresult}}\\
     \frac{1}{2} \& {\pgfmathparse{f(.5)}\pgfmathprintnumber{\pgfmathresult}}\\
    \frac{1}{4} \& {\pgfmathparse{f(.25)}\pgfmathprintnumber{\pgfmathresult}}\\
                                    };
      \node at ([yshift=-.8cm,xshift=2cm]mat1.south) 
            [circle,draw,font=\small,inner sep=1pt,label={[red]right:Select $y$}] (1){$1$};
      \node at ([yshift=-1.5cm,xshift=.7cm]mat1.south) 
            [circle,draw,font=\small,inner sep=1pt,label={[red]right:Compute
            $x$}] (2){$2$};        
     \draw[latex-] (mat1-7-1|-mat1.south) |- (2);
     \draw[latex-] (mat1-7-2|-mat1.south) |- (1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

离题(因为主题已由@marmot 回答解决),但它可能在编写矩阵时很方便......

  • 使用column 1/.append style={nodes={text width=15mm}(第 2 列类似),您可以|[text width=11mm]|从所有单元格中删除
  • \pgfmathtruncatemacro{\L}{f(<value>)}\L 而是使用\pgfmathparse{f(.5)}\pgfmathprintnumber{\pgfmathresult}稍微短一点的代码
  • 对于矩阵下方的节点我将定义通用样式
  • ampersand replacement=\&没有必要使用

    \documentclass{article}
    \usepackage{amsmath}
    \usepackage{tikz}
    \usetikzlibrary{matrix}
    
    \begin{document}
        \begin{tikzpicture}[
    declare function = {f(\x)=1/ln(2)*ln(\x);},
         circ/.style = {circle,draw,font=\small,inner sep=1pt, % <---
                        label={[red]right:#1}}                 % <--- 
                            ]
    \matrix[matrix of math nodes,
            nodes = {align=center,inner sep=3pt,
                     text height=2ex, text depth=1ex,         % <---
                     draw=gray!40,very thin},
            draw, inner sep=0pt,
            column 1/.append style={nodes={text width=15mm}},  % <---
            column 2/.append style={nodes={text width=9mm}}    % <---
            ] (mat1)
    {
    |[fill=green!40!gray]| x\text{, or }2^{y}                  % <---
        & |[fill=green!40!gray]| y              \\             % <---
    1   & {\pgfmathtruncatemacro{\L}{f(1)}\L}   \\             % <---
    2   & {\pgfmathtruncatemacro{\L}{f(2)}\L}   \\
    4   & {\pgfmathtruncatemacro{\L}{f(4)}\L}   \\
    8   & {\pgfmathtruncatemacro{\L}{f(8)}\L}   \\
    \frac{1}{2} & {\pgfmathtruncatemacro{\L}{f(0.5)}\L}     \\
    \frac{1}{4} & {\pgfmathtruncatemacro{\L}{f(0.25)}\L}    \\
    };
    \node at ([yshift=- 8mm] mat1.south east)   [circ=Select  $y$] (c1) {1}; % <---
    \node at ([yshift=-18mm] mat1-7-2)          [circ=Compute $x$] (c2) {2}; % <---
    \draw[latex-] (mat1-7-2) |- (c1);  % <---
    \draw[latex-] (mat1-7-1) |- (c2);  % <---
        \end{tikzpicture}
    \end{document}
    

在此处输入图片描述

编辑:

  • 修正了箭头的方向
  • 添加了使用原始方式计算第二列值的版本,即使用\pgfmathparse{f(1)}\pgfmathprintnumber{\pgfmathresult}(正如 marmot 在他的回答中指出的,它可以计算实际值),为其定义了新命令\calculation

    \documentclass[tikz, margin=3mm]{standalone}
    \usetikzlibrary{matrix}
    \usepackage{amsmath}
        \begin{tikzpicture}[
    declare function = {f(\x)=1/ln(2)*ln(\x);},
         circ/.style = {circle,draw,font=\small,inner sep=1pt,  % <---
                        label={[red]right:#1}}                  % <---
                            ]
    \newcommand\calculation[1]{\pgfmathparse{f(#1)}%
                               \pgfmathprintnumber{\pgfmathresult}}  % <---
    \matrix[matrix of math nodes,
            nodes = {text height=1.5ex, text depth=0.5ex,       % <---
                     align=center, inner ysep=4pt,
                     draw=gray!40,very thin},
            draw, inner sep=0pt,
            column 1/.append style={nodes={text width=15mm}},   
            column 2/.append style={nodes={text width=11mm}},   
            row 1/.style = {nodes={fill=green!40!gray}}         % <---
            ] (mat1)
    {
    x\text{, or }2^{y}                                          % <---
        & y                             \\                      % <---
    1   & {\calculation{1}}             \\                      % <---
    2   & {\calculation{2}}             \\
    4   & {\calculation{4}}             \\
    8   & {\calculation{8}}             \\
    \frac{1}{2} & {\calculation{0.5}}   \\
    \frac{1}{4} & {\calculation{0.25}}  \\
    };
    \node at ([yshift=- 8mm] mat1.south east)   [circ=Select  $y$] (c1) {1}; % <---
    \node at ([yshift=-18mm] mat1-7-2)          [circ=Compute $x$] (c2) {2}; % <---
    \draw[latex-] (mat1-7-2) |- (c1);  % <---
    \draw[latex-] (mat1-7-1) |- (c2);  % <---
        \end{tikzpicture}
    \end{document}
    

结果和上面一样。

相关内容