使用 Tikzmark 进行标记

使用 Tikzmark 进行标记

我需要一些帮助来恰当地放置标签。

\documentclass{article}     
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{colortbl} %%% ALLOWS COLORING OF CELL IN TABULAR ENVIRONMENT
\usepackage{amsmath}
\usepackage{hhline}
\begin{document}
\begin{center}
\begin{tabular}{|cc|p{1in}|cc|p{1in}|cc|} \hhline{--~--~--}
\multicolumn{2}{|c|}{\tikzmarknode{A}{\cellcolor{green!50!white}U.S.}} 
 &&\multicolumn{2}{c|}{\tikzmarknode{B} 
{}\cellcolor{green!50!white}France}&&\multicolumn{2}{c|} 
{\cellcolor{green!50!white}Japan} \\ [5pt] \hhline{--~--~--}
 \multicolumn{2}{|c|}{$8$} &&\multicolumn{2}{c|}{$41\frac{1} 
{3}$}&&\multicolumn{2}{c|}{$260$} \\ [5pt]%\hhline{--~--~--}
 \multicolumn{2}{|c|}{$9$} &&\multicolumn{2}{c|}{$42\frac{2} 
{3}$}&&\multicolumn{2}{c|}{$270$} \\ [5pt]%\hhline{--~--~--}
\multicolumn{2}{|c|}{$10$} &&\multicolumn{2}{c|}{$44$}&&\multicolumn{2}{c|} 
 {$280$} \\ [5pt]%\hhline{--~--~--}
\multicolumn{2}{|c|}{$11$} &&\multicolumn{2}{c|}{$45\frac{1} 
{3}$}&&\multicolumn{2}{c|}{$290$} \\ [5pt]%\hhline{--~--~--}
\multicolumn{2}{|c|}{$11\frac{1}{2}$} &&\multicolumn{2}{c|} 
{$46$}&&\multicolumn{2}{c|}{$295$} \\ [5pt] \hhline{--~--~--}
\end{tabular}
\end{center}

\begin{tikzpicture}[overlay,remember picture]
\draw[red,-latex] ([yshift=5pt,xshift=-5pt]A.north) -- ++  (0pt,24pt) 
 coordinate (aux)  -|  ([yshift=9pt,xshift=7pt]B.north) ;
\end{tikzpicture}
\end{document}

输出:

enter image description here

我正在尝试制作的标签:

enter image description here

答案1

对我来说,将所有东西画成图似乎更简单tikz

\documentclass[tikz, margin=3.14192]{standalone}
\usetikzlibrary{arrows.meta, matrix}
\usepackage{nccmath}   % for used \mfrac

\begin{document}
    \begin{tikzpicture}[
FIT/.style = {draw, inner sep=0pt, outer sep=0pt, fit=#1},
arr/.style = {red, semithick, -{Triangle[length=0pt 4,width=0pt 4]},
              shorten >=1mm, shorten <=1mm }
                        ]
\matrix (m) [matrix of math nodes,
             nodes={minimum width=3em, minimum height=4.5ex,
                    inner ysep=1pt, inner xsep=2pt, anchor=center},
             column sep=22mm,
             row sep =-\pgflinewidth,
             row 1/.style = {nodes={draw, fill=olive!25}}
             ]
{
\text{US}       &   \text{France}   &   \text{Japan}    \\
8               &   41\mfrac{1}{3}  &   260             \\
9               &   42\mfrac{2}{3}  &   270             \\
10              &   44              &   280             \\
11              &   45\mfrac{1}{3}  &   290             \\
11\mfrac{1}{2}  &   46              &   295             \\
};
\draw[arr]  (m-1-1.north) -- ++  (0pt,24pt) -|
    node[pos=0.25,below] {$g(x)=\dfrac{4x+92}{3}$}
    node[pos=0.25,text=cyan, above] {$g$}
            ([ xshift=-1ex] m-1-2.north) ;
\draw[arr]  ([xshift=1ex]m-1-2.north) -- ++  (0pt,24pt) -|
    node[pos=0.25,below] {$f(x)=\dfrac{15x-100}{2}$}
    node[pos=0.25,text=cyan, above] {$f$}
            (m-1-3) ;
\draw[arr]  (m-6-1.south) -- ++  (0pt,-24pt) -|
    node[pos=0.25,below] {$h(x)=?$}
    node[pos=0.25,text=cyan, above] {$h$}
            (m-6-3) ;
\draw   (m-1-1.south west) |- (m-6-1.south east) -- (m-1-1.south east)
        (m-1-2.south west) |- (m-6-2.south -| m-1-2.east) -- (m-1-2.south east)
        (m-1-3.south west) |- (m-6-3.south east) -- (m-1-3.south east);
    \end{tikzpicture}
\end{document}

这使:

enter image description here

在代码中使用了两个tikz库:arrows.meta用于箭头and矩阵的库,用它来替换你的表。

附录:离题了,但结果可能很有趣 :-)。使用\nicefrac同名包中的,图像变得更紧凑,也更符合我的口味:

\documentclass[tikz, margin=3.14192]{standalone}
\usetikzlibrary{arrows.meta, matrix}
\usepackage{amsmath, nicefrac}   % for nice frac

\begin{document}
    \begin{tikzpicture}[
FIT/.style = {draw, inner sep=0pt, outer sep=0pt, fit=#1},
arr/.style = {red, semithick, -{Triangle[length=0pt 4,width=0pt 4]},
              shorten >=1mm, shorten <=1mm }
                        ]
\matrix (m) [matrix of math nodes,
             nodes={minimum width=3em, minimum height=3.5ex,
                    inner ysep=1pt, inner xsep=2pt, anchor=center},
             column sep=22mm,
             row sep =-\pgflinewidth,
             row 1/.style = {nodes={draw, fill=olive!25}}
             ]
{
\text{US}           &   \text{France}       &   \text{Japan}    \\
8                   &   41\nicefrac{1}{3}   &   260             \\
9                   &   42\nicefrac{2}{3}   &   270             \\
10                  &   44                  &   280             \\
11                  &   45\nicefrac{1}{3}   &   290             \\
11\nicefrac{1}{2}   &   46                  &   295             \\
};
\draw[arr]  (m-1-1.north) -- ++  (0pt,24pt) -|
    node[pos=0.25,below] {$g(x)=\dfrac{4x+92}{3}$}
    node[pos=0.25,text=cyan, above] {$g$}
            ([ xshift=-1ex] m-1-2.north) ;
\draw[arr]  ([xshift=1ex]m-1-2.north) -- ++  (0pt,24pt) -|
    node[pos=0.25,below] {$f(x)=\dfrac{15x-100}{2}$}
    node[pos=0.25,text=cyan, above] {$f$}
            (m-1-3) ;
\draw[arr]  (m-6-1.south) -- ++  (0pt,-24pt) -|
    node[pos=0.25,below] {$h(x)=?$}
    node[pos=0.25,text=cyan, above] {$h$}
            (m-6-3) ;
\draw   (m-1-1.south west) |- (m-6-1.south east) -- (m-1-1.south east)
        (m-1-2.south west) |- (m-6-2.south -| m-1-2.east) -- (m-1-2.south east)
        (m-1-3.south west) |- (m-6-3.south east) -- (m-1-3.south east);
    \end{tikzpicture}
\end{document}

enter image description here

答案2

tikzmark使用普通表格仍能得到不错的结果。表格条目通过表格标题简化,>{$}c<{$}因此数学模式成为默认模式。请注意,绘制箭头时,从同一节点开始,(f)以使线条处于同一高度。另外,我借用了@Zarko 的 nice olive!25

\documentclass{article}
\usepackage{colortbl,amsmath,tikz,hhline} 
\usetikzlibrary{tikzmark}
\begin{document}

\begin{tabular}[t]{|>{$}c<{$}|}
\hhline{-} \tikzmarknode{u}{\cellcolor{olive!25}\text{U.S.}}   \\ \hhline{-} 8 \\ 9 \\ 10 \\ 11 \\ \tikzmarknode{bl}{11\frac12} \\ \hline
\end{tabular}\hspace{5em}
\begin{tabular}[t]{|>{$}c<{$}|}
\hhline{-} \tikzmarknode{f}{\cellcolor{olive!25}\text{France}} \\ \hhline{-} 8 \\ 9 \\ 10 \\ 11 \\ 11\frac12 \\ \hline
\end{tabular}\hspace{5em}
\begin{tabular}[t]{|>{$}c<{$}|}
\hhline{-} \tikzmarknode{j}{\cellcolor{olive!25}\text{Japan}}  \\ \hhline{-} 8 \\ 9 \\ 10 \\ 11 \\ \tikzmarknode{br}{11\frac12} \\ \hline
\end{tabular}

\begin{tikzpicture}[remember picture, overlay, shorten >=.5em, shorten <=.5em]
\draw[<-] (f.120) -- ++(0,3em) -| node[above,pos=.25]{$g$}node[below,pos=.25]{$g(x)=\dfrac{4x+92}{3}$}(u);
\draw[->] (f.60) -- ++(0,3em) -| node[above,pos=.25]{$f$}node[below,pos=.25]{$f(x)=\dfrac{15x-100}{2}$}(j);
\draw[->] (bl) -- ++(0,-3em) -| node[above,pos=.25]{$h$}node[below,pos=.25]{$h(x)=?$}(br);
\end{tikzpicture}

\end{document}

enter image description here

答案3

tikzmark更新:添加了第二个解决方案,但其中仍然没有任何包裹 :-)

它使用shapes.multipart允许创建由宏分隔的两个部分组成的矩形的库\nodepart{two}

这样就可以对矩形的不同部分分别着色。为此,我编写了样式table

table/.style={rectangle split,rectangle split parts=2,
rectangle split part fill={green!50,white}}

我为桥梁样式添加了两个默认值。

bridge/.default={north}{11mm},

结果和上一个解决方案完全相同,完整代码为:

\documentclass{article}     
\usepackage{tikz}
\usetikzlibrary{positioning,quotes,shapes.multipart}
\usepackage{colortbl} 
\usepackage{amsmath}
\renewcommand{\arraystretch}{1.5}
\tikzset{bridge/.style 2 args= {->,red,near start,auto,to path={([xshift=4pt]\tikztostart.#1)-- ++(0,#2) -| ([xshift=-4pt]\tikztotarget)\tikztonodes}},
bridge/.default={north}{11mm},
table/.style={rectangle split,rectangle split parts=2,
rectangle split part fill={green!50,white}}
}
\begin{document}
\begin{tikzpicture}[node distance=2cm,
]
\node[draw,table](US){
U.S.
\nodepart{two}
\begin{tabular}{c}
8\\
9\\
10\\
11\\
$11\frac{1}{2}$\\
\end{tabular}
};

\node[draw,table,right=of US](France){
France\\
\nodepart{two}
\begin{tabular}{c}
$41\frac{1} {3}$\\
$42\frac{2} {3}$\\
44\\
$45\frac{1} {3}$\\
46\\
\end{tabular}
};

\node[draw,table,right=of France](Japan){
Japan\\
\nodepart{two}
\begin{tabular}{c}
260\\
270\\
280\\
290\\
295\\
\end{tabular}
};

\draw[bridge] (US)to ["g",text=blue] node[swap,text=red] {$g(x)=\dfrac{4x+92}{3}$}(France);
\draw[bridge] (France) to ["f",text=blue] node[swap,text=red] {$g(x)=\dfrac{15x-100}{2}$}(Japan);
\draw[bridge={south}{-5mm}] (US)to ["h",text=blue]node[swap,text=red]{$h(x)=?$}(Japan);
\end{tikzpicture}

\end{document}

旧答案:只是为了好玩,这是一个纯 TikZ 解决方案。

而且其中没有一个优秀的 tikzmark 包:-)

可以tabular使用 TikZ 在节点中放置环境(参见手册 3.0.1a 第 223 页). 一旦完成,每个节点就以经典方式连接。

在这里,为了方便使用styles操作来操作TikZ to path,我编写了一个bridge包含 2 个参数的样式:

  • 第一个是anchor箭头指向的那个,
  • 第二个是height桥梁。

tableaux

\documentclass{article}     
\usepackage{tikz}
\usetikzlibrary{positioning,quotes}
\usepackage{colortbl} 
\usepackage{amsmath}
\renewcommand{\arraystretch}{1.5}
\tikzset{bridge/.style 2 args= {->,red,near start,auto,to path={([xshift=4pt]\tikztostart.#1)-- ++(0,#2) -| ([xshift=-4pt]\tikztotarget)\tikztonodes}},
}
\begin{document}
\begin{tikzpicture}[node distance=2cm]
\node[](US){
\begin{tabular}{|c|}
  \hline
 \cellcolor{green!50!white}U.S.\\
  \hline
8\\
9\\
10\\
11\\
$11\frac{1}{2}$\\
  \hline
\end{tabular}
};

\node[right=of US](France){
\begin{tabular}{|c|}
  \hline
 \cellcolor{green!50!white}France\\
  \hline
$41\frac{1} {3}$\\
$42\frac{2} {3}$\\
44\\
$45\frac{1} {3}$\\
46\\
  \hline
\end{tabular}
};

\node[right=of France](Japan){
\begin{tabular}{|c|}
  \hline
 \cellcolor{green!50!white}Japan\\
  \hline
260\\
270\\
280\\
290\\
295\\
  \hline
\end{tabular}
};

\draw[bridge={north}{11mm}] (US)to ["g",text=blue] node[swap,text=red] {$g(x)=\dfrac{4x+92}{3}$}(France);
\draw[bridge={north}{11mm}] (France) to ["f",text=blue] node[swap,text=red] {$g(x)=\dfrac{15x-100}{2}$}(Japan);
\draw[bridge={south}{-5mm}] (US)to ["h",text=blue]node[swap,text=red]{$h(x)=?$}(Japan);
\end{tikzpicture}

\end{document}

使用 www.DeepL.com/Translator 翻译

相关内容