Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to fix the broken placement of macros in matrix?

\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}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Is this what you wanted to do?

\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}

enter image description here

EDIT In case anyone is interested, after our chat session, we were able to define a new shape for the pinode as follows:

\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}
share|improve this answer
    
yes, thank you :). As I see, I have to dig the pgf code due I'm trying solve all problems just with TikZ macros of pgf code. –  Berso Mar 11 '12 at 16:39
    
No problem, it's actually not that different than what you have done. I only took out the extra tikzpicture and \tikz environments and made the declarations more local. –  percusse Mar 11 '12 at 16:58
    
If you don't mind and have some time I would like show you and get you into my complete problem what I need to finish in LaTeX - wont post it here as problem/question coz it will not meet the minimal requirements. If you agree or anybody else just contact me on gmail - jezkovivocihorimpreboha@gmail.com . Will appreciate much. –  Berso Mar 11 '12 at 17:10
    
I can't promise to spare too much time but if it is like an image or similar, you can upload it on imgur and post it on the chat. Make sure to put @percusse in the beginning of your message to ping me when you are done. Then others and I can try to have a look at it. Or if it can be isolated you can keep asking concrete and specialized questions. We are here to help anyway. –  percusse Mar 11 '12 at 17:25
    
Thanks, but can't post anything right now on the chat - 2 points needed and you know ehm..I wont look like some annoying dumb person. :\ And I prefer kind of communication what lefts history,closer chats, because it's important work what I need to be done for my university. –  Berso Mar 11 '12 at 19:13
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.