如何在 /tabular{} 定义中应用 Tikz 样式

如何在 /tabular{} 定义中应用 Tikz 样式

我刚刚开始使用 Tikz,发现它很棒 - 但也有点不知所措。所以这是一个绝对初学者的问题:

我复制了此表格给某人:

% Draw inside tables:
% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[inner sep=0pt,anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]
 here

它的目的是在 LaTeX 表中的表格或列周围绘制圆圈,如下所示(有一个定义的计数器,因此我可以使用数字来引用选项卡节点):

\begin{table}[htbp]
\begin{tabular}{ ...
\tabnode{$null?(s)$} & \tabnode{true} & \tabnode{false} ...
\end{tabular}
\begin{tikzpicture}[overlay]
% Define the circle paths
\draw [black](3.north west) -- (5.north east) -- 
(5.south east) -- (3.south west)  -- cycle;
...
\end{tikzpicture}
\end{table}

一切都很顺利,直到我开始向文档中添加其他独立的 Tikz 图形。当然,“所有节点和路径通用的选项”会引发问题,因为它们不应该也适用于新的独立图形。

所以我的问题是:将“每张图片”、“每个节点”、“每条路径”定义更改为“overlay_picture”、“overlay_node”和“overlay_path”后,如何将这些新定义应用于表定义?

编辑:

(1)表格叠加有效,独立图形无效:

\documentclass[german, a4paper, table]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows}
\usepackage{tkz-graph}
\usepackage{tabularx}


%% (1) Draw inside tables :
%% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[inner sep=0pt,anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]

%% (2) Draw inside tables :
% \tikzset{mytablestyle/.style={% Now everything inside these braces are local to mytablestyle
%          every picture/.style={remember picture,baseline},
%          every node/.append style={
%                          inner sep=0pt,anchor=base,
%                          minimum width=1.8cm,align=center,
%                          text depth=.25ex,outer sep=1.5pt},
%                          every path/.append style={thick, rounded corners}
%         }
% }


% tikz definitions for other, independent pictures
\tikzstyle{abstract}=[rectangle, draw=black, rounded corners,
fill=blue!40, drop shadow, text centered, anchor=north, text=white,
text width=3cm]

\tikzstyle{comment}=[rectangle, draw=black, rounded corners,
fill=green, drop shadow, text centered, anchor=north, text=white, text
width=3cm]

\tikzstyle{myarrow}=[->, >=open triangle 90, shorten >=3mm, thick]

\tikzstyle{line}=[-, thick]


\begin{document} 

%%% table with overlay
% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount counter
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node (\arabic{nodecount}) {#1};}

% table
\begin{table}[htbp]
  \centering
\begin{tabular}{ l@{\hspace{8mm}} !{\vrule width 1.5pt}
    @{\hspace{10mm}} c@{\hspace{10mm}} c@{\hspace{10mm}}}
  & \multicolumn{2}{l}{\large{constructor of s}} \\
  \large{observations} & \tabnode{$nil$} & \tabnode{$adjoin(s', n)$}\\ 
    \noalign{\hrule height 1.5pt}
\tabnode{$null?(s)$} & \tabnode{true} & \tabnode{false} \\
\tabnode{$head(s)$} & \tabnode{\textbf{error}} & \tabnode{n\vphantom{h}}\\ 
\tabnode{$tail(s)$} & \tabnode{\textbf{error}} & \tabnode{s'} \\ 
\end{tabular}
\begin{tikzpicture}[overlay]
% Define the circle paths
\draw [black](3.north west) -- (5.north east) -- 
(5.south east) -- (3.south west)  -- cycle;
\draw [black](6.north west) -- (8.north east) -- 
(8.south east) -- (6.south west)  -- cycle;
\draw [black](9.north west) -- (11.north east) -- 
(11.south east) -- (9.south west)  -- cycle;
\end{tikzpicture}
\end{table}


% independent tikz graphic
\begin{figure}[htp]
 \centering
\begin{tikzpicture}[node distance=2cm]
    \node (Fahrzeug) [abstract, rectangle split, rectangle split parts=2]
        {
            \textbf{Fahrzeug}
            \nodepart{second}fortbewegen()
        };
    \node (Landfahrzeug) [abstract, rectangle split, rectangle split
    parts=2, below left=of Fahrzeug]
        {
            \textbf{Landfahrzeug}
            \nodepart{second}override \\ fortbewegen()
        };
    \node (Wasserfahrzeug) [abstract, rectangle split, rectangle split
    parts=2, below right=of Fahrzeug]
        {
            \textbf{Wasserfahrzeug}
            \nodepart{second}override \\ fortbewegen()
        };

   \node (Amphibienfahrzeug) [abstract, rectangle split, rectangle
   split parts=2, below right=of Landfahrzeug]
        {
          \textbf{Amphibien-}\\
          \textbf{fahrzeug}
            \nodepart{second}\phantom{()}
        };

    \draw[myarrow] (Landfahrzeug.north) --  (Fahrzeug.south);
    \draw[myarrow] (Wasserfahrzeug.north) --  (Fahrzeug.south);
    \draw[myarrow] (Amphibienfahrzeug.north) --  (Landfahrzeug.south);
    \draw[myarrow] (Amphibienfahrzeug.north) --  (Wasserfahrzeug.south);
\end{tikzpicture}
\end{figure}
\end{document} 

输出:

表格覆盖工作

(2)独立图形可以工作,但表格覆盖则不行:

\documentclass[german, a4paper, table]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows}
\usepackage{tkz-graph}
\usepackage{tabularx}


%% (1) Draw inside tables :
%% Some options common to all the nodes and paths
% \tikzstyle{every picture}+=[remember picture,baseline]
% \tikzstyle{every node}+=[inner sep=0pt,anchor=base,
% minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
% \tikzstyle{every path}+=[thick, rounded corners]

% (2) Draw inside tables :
\tikzset{mytablestyle/.style={% Now everything inside these braces are local to mytablestyle
         every picture/.style={remember picture,baseline},
         every node/.append style={
                         inner sep=0pt,anchor=base,
                         minimum width=1.8cm,align=center,
                         text depth=.25ex,outer sep=1.5pt},
                         every path/.append style={thick, rounded corners}
        }
}


% tikz definitions for other, independent pictures
\tikzstyle{abstract}=[rectangle, draw=black, rounded corners,
fill=blue!40, drop shadow, text centered, anchor=north, text=white,
text width=3cm]

\tikzstyle{comment}=[rectangle, draw=black, rounded corners,
fill=green, drop shadow, text centered, anchor=north, text=white, text
width=3cm]

\tikzstyle{myarrow}=[->, >=open triangle 90, shorten >=3mm, thick]

\tikzstyle{line}=[-, thick]


\begin{document} 

%%% table with overlay
% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount counter
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node (\arabic{nodecount}) {#1};}

% table
\begin{table}[htbp]
  \centering
\begin{tabular}{ l@{\hspace{8mm}} !{\vrule width 1.5pt}
    @{\hspace{10mm}} c@{\hspace{10mm}} c@{\hspace{10mm}}}
  & \multicolumn{2}{l}{\large{constructor of s}} \\
  \large{observations} & \tabnode{$nil$} & \tabnode{$adjoin(s', n)$}\\ 
    \noalign{\hrule height 1.5pt}
\tabnode{$null?(s)$} & \tabnode{true} & \tabnode{false} \\
\tabnode{$head(s)$} & \tabnode{\textbf{error}} & \tabnode{n\vphantom{h}}\\ 
\tabnode{$tail(s)$} & \tabnode{\textbf{error}} & \tabnode{s'} \\ 
\end{tabular}
\begin{tikzpicture}[overlay]
% Define the circle paths
\draw [black](3.north west) -- (5.north east) -- 
(5.south east) -- (3.south west)  -- cycle;
\draw [black](6.north west) -- (8.north east) -- 
(8.south east) -- (6.south west)  -- cycle;
\draw [black](9.north west) -- (11.north east) -- 
(11.south east) -- (9.south west)  -- cycle;
\end{tikzpicture}
\end{table}


% independent tikz graphic
\begin{figure}[htp]
 \centering
\begin{tikzpicture}[node distance=2cm]
    \node (Fahrzeug) [abstract, rectangle split, rectangle split parts=2]
        {
            \textbf{Fahrzeug}
            \nodepart{second}fortbewegen()
        };
    \node (Landfahrzeug) [abstract, rectangle split, rectangle split
    parts=2, below left=of Fahrzeug]
        {
            \textbf{Landfahrzeug}
            \nodepart{second}override \\ fortbewegen()
        };
    \node (Wasserfahrzeug) [abstract, rectangle split, rectangle split
    parts=2, below right=of Fahrzeug]
        {
            \textbf{Wasserfahrzeug}
            \nodepart{second}override \\ fortbewegen()
        };

   \node (Amphibienfahrzeug) [abstract, rectangle split, rectangle
   split parts=2, below right=of Landfahrzeug]
        {
          \textbf{Amphibien-}\\
          \textbf{fahrzeug}
            \nodepart{second}\phantom{()}
        };

    \draw[myarrow] (Landfahrzeug.north) --  (Fahrzeug.south);
    \draw[myarrow] (Wasserfahrzeug.north) --  (Fahrzeug.south);
    \draw[myarrow] (Amphibienfahrzeug.north) --  (Landfahrzeug.south);
    \draw[myarrow] (Amphibienfahrzeug.north) --  (Wasserfahrzeug.south);
\end{tikzpicture}
\end{figure}
\end{document} 

经过两次编译后,以下是输出结果(抱歉,输出图形太大):

独立图形工作,不叠加

答案1

您可能在命令前的任何地方的序言中都有这些声明\begin{document}。因此这些设置将应用于每个 TikZ 图片。您可以做的是为那些表格和相关部分创建自定义样式,这些样式到目前为止一直有效。

一个例子是

\tikzset{mytablestyle/.style={% Now everything inside these braces are local to mytablestyle
         every picture/.style={remember picture,baseline},
         every node/.append style={
                         inner sep=0pt,anchor=base,
                         minimum width=1.8cm,align=center,
                         text depth=.25ex,outer sep=1.5pt
                         },
                         every path/.append style={thick, rounded corners}
        }
}

然后您可以将此样式包含在表格相关图片中,其余部分不会受到影响。

您可能在命令定义中也有一个\tikz命令或。通过或添加此样式\begin{tikzpicture}tabnode\tikz[mytablestyle]

\begin{tikzpicture}[mytablestyle]
....
\begin{tikzpicture}

它们将继续工作,但图片的整体风格不会受到影响。要了解我使用的原因\tikzset以及您的示例的原因\tikzstyle,请参阅应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?

相关内容