如何将宏对齐到 TikZ 矩阵中的正确顺序?

如何将宏对齐到 TikZ 矩阵中的正确顺序?

如何修复矩阵中宏的损坏位置?

\documentclass{article}
\usepackage{tikz}

\begin{document}

\newcommand{\macroA}{
\begin{tikzpicture}
\node[minimum height=1.5cm,minimum width=1.5cm, draw, rectangle] at (0,0) {PI};
\draw (0.2,-0.75)--(0.75,0)--(0.2,0.75);
\end{tikzpicture}}

\newcommand{\macroB}[1]
{       
        \tikz[baseline,inner sep=1pt]
        {
                \node[draw,shape=circle] {#1};
                }
}

\begin{figure}
\begin{tikzpicture}
[auto,
block/.style ={rectangle, draw, thick,text centered, minimum height=0.7cm,minimum width=1.4cm}]
\matrix [column sep=5mm,row sep=7mm]
{
% row 1
\node [block](a) {a}; &
\node [block](b) {b}; &
\node [block](c) {c}; \\
% row 2
\node [block](d) {d}; & \\
% row 3
\node [block](e) {e}; &
\node [block](f) {f}; & \\
% row 4
\node [block](g) {g}; & \\
% row 5
\node [block](h) {h}; & \\
};
\end{tikzpicture}
\end{figure}

\begin{figure}
\begin{tikzpicture}
\matrix [column sep=1cm,row sep=1cm]
{
% row 1
\macroA &
\macroB{X} &
\macroA \\
% row 2
\macroA;  & \\
% row 3
\macroB{X} &
\macroB{X}  & \\
% row 4
\macroB{X}  & \\
% row 5
\macroB{X}  & \\
};
\end{tikzpicture}
\end{figure}
\end{document}

答案1

这就是你想要做的吗?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{pinode/.code={
\pgfset{inner sep=7.5mm}
\pgfnode{rectangle}{base}{PI}{x}{\pgfusepath{stroke}}
\pgfpathmoveto{\pgfpointadd{\pgfpoint{2mm}{0}}{\pgfpointanchor{x}{north}}}
\pgfpathlineto{\pgfpointanchor{x}{east}}
\pgfpathlineto{\pgfpointadd{\pgfpoint{2mm}{0}}{\pgfpointanchor{x}{south}}}
\pgfusepath{stroke}
}
}

\newcommand{\macroA}{|[pinode]|}
\newcommand{\macroB}[1]{|[draw,circle]|#1}

\begin{document}

\begin{figure}
\begin{tikzpicture}[auto,block/.style ={
                                        rectangle, 
                                        draw, 
                                        thick,
                                        text centered, 
                                        minimum height=0.7cm,
                                        minimum width=1.4cm,
                                        inner sep=0,
                                        outer sep=0,anchor=center
                                        }
                                        ]
\matrix [matrix of nodes,
                nodes={block},
                column sep=5mm,
                row sep=7mm]
{
% row 1
a&b&c\\    % row 2
d& & \\    % row 3
e&f& \\    % row 4
g& & \\    % row 5
h& &\\    };
\end{tikzpicture}
\end{figure}

\begin{figure}
\begin{tikzpicture}
\matrix [matrix of nodes,column sep=1cm,row sep=1cm]
{
% row 1
\macroA &\macroB{X} &\macroA \\    % row 2
\macroA & & \\    % row 3
\macroB{X}&\macroB{X}  & \\    % row 4
\macroB{X}  & &\\    % row 5
\macroB{X}  & &\\    };
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

编辑如果有人感兴趣的话,在我们的聊天会话之后,我们可以定义一个新的形状,如下pinode所示:

\documentclass{article}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{pinode}{
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{east}
\backgroundpath{%
\pgfpathrectanglecorners{\southwest}{\northeast}
\northeast \pgf@xa=0\pgf@x \pgf@ya=\pgf@y \pgf@xb=\pgf@x
\southwest \pgf@yc = \pgf@y
\advance \pgf@xa by .9\wd\pgfnodeparttextbox
\advance \pgf@yb by .5\ht\pgfnodeparttextbox
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yc}}
\pgfusepath{stroke}
}
}
\makeatother

\begin{document}
\begin{tikzpicture}
\draw[opacity=0.2,style=help lines] (0,0) grid[step=1cm] (5cm,5cm);
\node [draw,pinode,inner sep=5mm] (a) at (0,2) {PI};
\node [draw,pinode,inner sep=5mm] (b) at (4,5) {PI};
\draw[->,thick] (a.east)-| ++ (1cm,1cm) |- (b.west);
\end{tikzpicture}
\end{document}

相关内容