节点样式定义问题

节点样式定义问题

我创建了一些节点样式,这些样式的灵感来自于马尔特·施密茨。正如您在 MWE 中看到的,许多内容都是硬编码的 - 尤其是节点的节点定位texpdf。我的问题是:我必须在代码中更改什么才能使用 \node … (tex) --+ (2,0) …

梅威瑟:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\setbeamertemplate{navigation symbols}{}
\tikzset{
    tex/.style={
        append after command={
            \pgfextra{
                    \node[minimum width=1.5cm,minimum height=1.5cm] (a) at (\tikzlastnode) {};
                    \draw[green!50!black,thick] ([xshift=-.4cm]a.north east) -- ([xshift=-.4cm,yshift=-.4cm]a.north east) -- ([yshift=-.4cm]a.north east);
                    \node[white,fill=green!50!black] at ([yshift=-.4cm]a.north west) (tex) {\sf TEX};
                    \draw[thick,green!50!black] (a.north west) -- (tex.north);
                    \draw[thick,green!50!black] (tex.south) -- (a.south west) -- (a.south east) -- ([yshift=-.4cm]a.north east) --  ([xshift=-.4cm]a.north east) -- ([xshift=-.014cm]a.north west);
                    \draw[shorten <=.2cm,thick,green!50!black] ([yshift=-.8cm]a.north west) -- ([xshift=-.2cm,yshift=-.8cm]a.north east) -- ([xshift=-.2cm,yshift=-.9cm]a.north east) -- ([yshift=-.9cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=-.2cm]a.north east);
            }
        }
    },
    pdf/.style={
        append after command={
            \pgfextra{
                \node[minimum width=1.5cm,minimum height=1.5cm] (a) at (\tikzlastnode) {};
                \draw[orange!80!black,thick] ([xshift=-.4cm]a.north east) -- ([xshift=-.4cm,yshift=-.4cm]a.north east) -- ([yshift=-.4cm]a.north east);
                \node[white,fill=orange!80!black] at ([yshift=-.4cm]a.north west) (pdf) {\sf PDF};
                \draw[thick,orange!80!black] (a.north west) -- (pdf.north);
                \draw[thick,orange!80!black] (pdf.south) -- (a.south west) -- (a.south east) -- ([yshift=-.4cm]a.north east) --  ([xshift=-.4cm]a.north east) -- ([xshift=-.014cm]a.north west);
                \draw[shorten <=.2cm,thick,orange!80!black] ([yshift=-.8cm]a.north west) -- ([xshift=-.2cm,yshift=-.8cm]a.north east) -- ([xshift=-.2cm,yshift=-.9cm]a.north east) -- ([yshift=-.9cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=-.2cm]a.north east);
            }
        }
    },
    aux/.style={
        append after command={
            \pgfextra{
                \node[minimum width=1.5cm,minimum height=1.5cm] (a) at (\tikzlastnode) {};
                \draw[purple!50,thick] ([xshift=-.4cm]a.north east) -- ([xshift=-.4cm,yshift=-.4cm]a.north east) -- ([yshift=-.4cm]a.north east);
                \node[white,fill=purple!50] at ([yshift=-.4cm]a.north west) (te) {\sf AUX};
                \draw[thick,purple!50] (a.north west) -- (te.north);
                \draw[thick,purple!50] (te.south) -- (a.south west) -- (a.south east) -- ([yshift=-.4cm]a.north east) --  ([xshift=-.4cm]a.north east) -- ([xshift=-.014cm]a.north west);
                \draw[shorten <=.2cm,thick,purple!50] ([yshift=-.8cm]a.north west) -- ([xshift=-.2cm,yshift=-.8cm]a.north east) -- ([xshift=-.2cm,yshift=-.9cm]a.north east) -- ([yshift=-.9cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=-.2cm]a.north east);
            }
        }
    },
    log/.style={
        append after command={
            \pgfextra{
                \node[minimum width=1.5cm,minimum height=1.5cm] (a) at (\tikzlastnode) {};
                \draw[thick] ([xshift=-.4cm]a.north east) -- ([xshift=-.4cm,yshift=-.4cm]a.north east) -- ([yshift=-.4cm]a.north east);
                \node[white,fill=black] at ([yshift=-.4cm]a.north west) (log) {\sf LOG};
                \draw[thick] (a.north west) -- (log.north);
                \draw[thick] (log.south) -- (a.south west) -- (a.south east) -- ([yshift=-.4cm]a.north east) --  ([xshift=-.4cm]a.north east) -- ([xshift=-.014cm]a.north west);
                \draw[shorten <=.2cm,thick] ([yshift=-.8cm]a.north west) -- ([xshift=-.2cm,yshift=-.8cm]a.north east) -- ([xshift=-.2cm,yshift=-.9cm]a.north east) -- ([yshift=-.9cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=-.2cm]a.north east);
            }
        }
    }
}
\begin{document}
    \begin{frame}
        \begin{tikzpicture}
            \scalebox{1.1}[1.1]{\node[tex,yshift=-6cm] (r) {};}
            \draw[very thick,->] ([xshift=.9cm,yshift=-.5cm]r.east) --+ (2,0) node[right] (s) {\bf\textrm{pdf\TeX}};
            \draw[very thick,->] (s) --+ (2,0);
                \node[pdf,right] at ($(s)+(3.5,0)$) {};
            \uncover<2>{
                \draw[very thick,->] (s) --+ (-1,-2);
                \draw[very thick,->] (s) --+ (1,-2);
                    \node[xshift=-.5cm,yshift=-1cm,aux] at ($(s)+(-1,-2)$) {};
                    \node[xshift=.5cm,yshift=-1cm,log] at ($(s)+(1,-2)$) {};
            }
            \uncover<3->{
                \begin{scope}[opacity=.5]
                    \draw[very thick,->] (s) --+ (-1,-2);
                    \draw[very thick,->] (s) --+ (1,-2);
                        \node[xshift=-.5cm,yshift=-1cm,aux] (c1) at ($(s)+(-1,-2)$) {};
                        \node[xshift=.5cm,yshift=-1cm,log] at ($(s)+(1,-2)$) {};
            \end{scope}
                \draw[very thick,->,shorten >=1cm] (0,-7.7) |- (2,-12) node[right=-1] (node) {\rm \textbf{pdf\TeX}};
                \draw[very thick,->] (node) --+ (2,0);
                \scalebox{1.1}[1.1]{\node[pdf] at (5,-11){};}
            }
        \end{tikzpicture}
    \end{frame}
\end{document}

PS:我知道 latex 文件的构建与演示中的不同,但这只是一种可视化。

输出:

截屏

截屏

截屏

答案1

以下是回答这个好问题的尝试。我消除了所有\pgfextra赞成 的path pictures,以及所有\scaleboxes赞成 的scale。此外,我认为有很多重复,所以我定义了一个generic节点样式,其他节点样式都从中派生出来。所有显式坐标都已被消除。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\setbeamertemplate{navigation symbols}{}
\tikzset{
    generic/.style args={color #1 and text #2}{minimum width=2.3cm,minimum height=1.54cm,
        path picture={
                    \node[minimum width=1.5cm,minimum height=1.5cm] (a) at (0,0) {};
                    \draw[#1,thick] ([xshift=-.4cm]a.north east) -- ([xshift=-.4cm,yshift=-.4cm]a.north east) -- ([yshift=-.4cm]a.north east);
                    \node[white,fill=#1,minimum height=5mm,minimum
                    width=9mm] at ([yshift=-.4cm]a.north west) (tex) {\sf #2};
                    \draw[thick,#1] (a.north west) -- (tex.north);
                    \draw[thick,#1] (tex.south) -- (a.south west) -- (a.south east) -- ([yshift=-.4cm]a.north east) --  ([xshift=-.4cm]a.north east) -- ([xshift=-.014cm]a.north west);
                    \draw[shorten <=.2cm,thick,#1] ([yshift=-.8cm]a.north west) -- ([xshift=-.2cm,yshift=-.8cm]a.north east) -- ([xshift=-.2cm,yshift=-.9cm]a.north east) -- ([yshift=-.9cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=-.2cm]a.north east);
        }
    },
    tex/.style={generic={color green!50!black and text {TEX}}},
    pdf/.style={generic={color orange!80!black and text {PDF}}},
    aux/.style={generic={color purple!50,thick and text {AUX}}},
    log/.style={generic={color black,thick and text {LOG}}},
}
\begin{document}
    \begin{frame}
        \begin{tikzpicture}
            \path (0,2);
            \node[tex,scale=1.1] (r) at (0,0) {};
            \draw[very thick,->] (r.east) --+ (2,0) node[right] (s) {\bf\textrm{pdf\TeX}};
            \draw[very thick,->] (s) --++ (2,0);
            \path (s) ++(4,0) node[pdf] {};
            \uncover<2>{
                \draw[very thick,->] (s) --+ (-1,-2);
                \draw[very thick,->] (s) --+ (1,-2);
                    \node[aux] at ($(s)+(-1.5,-3)$) {};
                    \node[log] at ($(s)+(1.5,-3)$) {};
            }
            \uncover<3->{
                \begin{scope}[opacity=.5]
                    \draw[very thick,->] (s) --+ (-1,-2);
                    \draw[very thick,->] (s) --+ (1,-2);
                        \node[aux] (c1) at ($(s)+(-1.5,-3)$) {};
                        \node[log] at ($(s)+(1.5,-3)$) {};
            \end{scope}}
            \uncover<4->{   
                \draw[very thick,->,shorten >=1cm] (r) |- ++(2,-6) node[right=-1] (node) {\rm \textbf{pdf\TeX}};
                \draw[very thick,->] (node) --+ (2,0);
                \path (node) ++ (3.5,0) node[pdf,scale=1.1] {};
            }            
    \end{tikzpicture}
    \end{frame}
\end{document}

在此处输入图片描述

但请注意,这path picture有一个缺点。环境节点的选项将部分影响路径图中的节点。这有时是好的,有时是坏的。在这种情况下,像这样的选项 right会扭曲节点。

避免这种情况的一种方法是使用\saveboxes。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\setbeamertemplate{navigation symbols}{}
\tikzset{
    generic/.style args={color #1 and text #2}{minimum width=2.3cm,minimum height=1.54cm,
        path picture={
                    \node[minimum width=1.5cm,minimum height=1.5cm] (a) at (0,0) {};
                    \draw[#1,thick] ([xshift=-.4cm]a.north east) -- ([xshift=-.4cm,yshift=-.4cm]a.north east) -- ([yshift=-.4cm]a.north east);
                    \node[white,fill=#1,minimum height=5mm,minimum
                    width=9mm] at ([yshift=-.4cm]a.north west) (tex) {\sf #2};
                    \draw[thick,#1] (a.north west) -- (tex.north);
                    \draw[thick,#1] (tex.south) -- (a.south west) -- (a.south east) -- ([yshift=-.4cm]a.north east) --  ([xshift=-.4cm]a.north east) -- ([xshift=-.014cm]a.north west);
                    \draw[shorten <=.2cm,thick,#1] ([yshift=-.8cm]a.north west) -- ([xshift=-.2cm,yshift=-.8cm]a.north east) -- ([xshift=-.2cm,yshift=-.9cm]a.north east) -- ([yshift=-.9cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=.2cm]a.north west) -- ([yshift=-1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=-.2cm]a.north east) -- ([yshift=-1.1cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=.2cm]a.north west) -- ([yshift=-1.2cm,xshift=-.2cm]a.north east);
        }
    },
    tex/.style={generic={color green!50!black and text {TEX}}},
    pdf/.style={generic={color orange!80!black and text {PDF}}},
    aux/.style={generic={color purple!50,thick and text {AUX}}},
    log/.style={generic={color black,thick and text {LOG}}},
}
\newsavebox\TEXBOX
\newsavebox\PDFBOX
\newsavebox\AUXBOX
\newsavebox\LOGBOX
\sbox\TEXBOX{\tikz{\node[tex]{};}}
\sbox\PDFBOX{\tikz{\node[pdf]{};}}
\sbox\AUXBOX{\tikz{\node[aux]{};}}
\sbox\LOGBOX{\tikz{\node[log]{};}}
\begin{document}
    \begin{frame}
        \begin{tikzpicture}
            \path (0,2);
            \node[scale=1.1] (r) at (0,0) {\usebox\TEXBOX};
            \draw[very thick,->] (r.east) --+ (2,0) node[right] (s) {\bf\textrm{pdf\TeX}};
            \draw[very thick,->] (s) --++ (2,0) node[right]{\usebox\PDFBOX};
            \uncover<2>{
                \draw[very thick,->] (s) --+ (-1,-2) node[below left] {\usebox\AUXBOX};
                \draw[very thick,->] (s) --+ (1,-2) node[below right] {\usebox\LOGBOX};
            }
            \uncover<3->{
                \begin{scope}[opacity=.5]
                    \draw[very thick,->] (s) --+ (-1,-2) node[below left] {\usebox\AUXBOX};
                    \draw[very thick,->] (s) --+ (1,-2) node[below right] {\usebox\LOGBOX};
                \end{scope}
                }
            \uncover<4->{   
                \draw[very thick,->,shorten >=1cm] (r) |- ++(2,-6) node[right=-1] (node) {\rm \textbf{pdf\TeX}};
                \draw[very thick,->] (node) --+ (2,0) node[right,scale=1.1] {\usebox\PDFBOX};
            }
        \end{tikzpicture}
    \end{frame}
\end{document}

在此处输入图片描述

编辑:成为硬编码距离的受害者,希望已经修复了这个问题。

附录:在我看来,最简洁的解决方案是使用形状,然后加载库overlay-beamer-styles。幸运的是,所需的形状几乎完全在这个很好的答案

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,overlay-beamer-styles}
\makeatletter
% https://tex.stackexchange.com/a/103800/121799
\pgfdeclareshape{document}{%
    \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{east}
    \inheritanchor[from=rectangle]{north west}
    \inheritanchor[from=rectangle]{south west}
    \inheritanchor[from=rectangle]{north east}
    \inheritanchor[from=rectangle]{south east}
    % ... and possibly more
    \backgroundpath{% this is new
        % store lower right in xa/ya and upper right in xb/yb
        \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        \def\hangout{12pt}
        % compute corner of "flipped page"
        \pgf@xc=\pgf@xb \advance\pgf@xc by-\hangout % this should be a parameter
        \pgf@yc=\pgf@yb \advance\pgf@yc by-\hangout
        % construct main path
        \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
        \pgfpathclose
        % add little corner
        \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
        \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
        \pgfpathclose
        % add lines
        \pgfmathtruncatemacro{\step}{abs(\pgf@yb-\pgf@ya)/6}
        \pgfmathtruncatemacro{\stepx}{abs(\pgf@yb-\pgf@ya)/12}
        \pgfmathtruncatemacro{\lines}{abs(\pgf@yb-\pgf@ya)/\step-1}
        \foreach \y in {1,...,\lines}{%
            \ifodd\y
              \pgfpathmoveto{\pgfpoint{\pgf@xa+\stepx*1pt+\hangout}{\[email protected]*(\lines+\y)*\step*1pt}}
              \pgfpathlineto{\pgfpoint{\pgf@xc-\stepx*1pt}{\[email protected]*(\lines+\y)*\step*1pt}}
              \ifnum\y=1
              \else
                \pgfpathlineto{\pgfpoint{\pgf@xc-\stepx*1pt}{\[email protected]*(\lines+\y-1)*\step*1pt}}
              \fi
            \else  
              \pgfpathmoveto{\pgfpoint{\pgf@xc-\stepx*1pt}{\[email protected]*(\lines+\y)*\step*1pt}}
              \pgfpathlineto{\pgfpoint{\pgf@xa+\stepx*1pt+\hangout}{\[email protected]*(\lines+\y)*\step*1pt}}
              \ifnum\y=1
              \else
                \pgfpathlineto{\pgfpoint{\pgf@xa+\stepx*1pt+\hangout}{\[email protected]*(\lines+\y-1)*\step*1pt}}
              \fi
            \fi
        }%  
    }%
}%
\makeatother
\setbeamertemplate{navigation symbols}{}
\begin{document}
\tikzset{doc/.style args={#1/#2}{%
draw=#1,
thick,
align=center,
shape=document,
minimum width=20mm,
minimum height=18mm,
label={[xshift=12pt,fill=#1,text=white,font=\sffamily]172:#2},
},
transparent on/.style={alt={#1{opacity=0.5}{}}},}
\begin{frame}[t]{Title}
\begin{tikzpicture}
   \path (0,2);
   \node[doc=green!50!black/TEX] (r) at (0,0){};
   \draw[very thick,->] (r) -- ++ (2.5,0) node[right] (s) {\bf\textrm{pdf\TeX}};
   \draw[very thick,->] (s) -- ++ (2.5,0) node[right,doc=orange!80!black/pdf]{};
   \uncover<2->{
      \draw[transparent on=<2>,very thick,->] (s) --++ (-1,-2) 
        node[below,xshift=-5mm,doc=purple!50/aux]{};
      \draw[transparent on=<2>,very thick,->] (s) --++ (1,-2) 
      node[below,xshift=5mm,doc=black/aux]{};
   }
   \uncover<4->{    
      \draw[very thick,->,shorten >=1cm] (r) |- ++(2,-6) node[right=-1] (node) 
      {\rm \textbf{pdf\TeX}};
      \draw[very thick,->] (node) --+ (2,0) node[right,doc=orange!80!black/pdf]{};
   }            
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容