3

The code in the previous response was terrific. Unfortunately, it created some new problems in my document that I didn't anticipate. I've pasted some updated code below to illustrate the problem. In addition to diagramming some mixed models, I want to show how a set of tracing rules can be applied to the diagrams to derive the mixed model equations. A bit of the text that shows how to do that has been incorporated at the bottom of the updated code.

This used to look fairly nice in the old code with 1.5 line spacing. The only problem was that the triangles for H11 and W11 tended to be a little too wide. With the updated code, they are also taller and the lines don't seem to have enough vertical spacing. Also, the triangles tend to be taller than other shapes like rectangles and that too is not esthetically pleasing.

Is there a way to revise the code so that the diagram still looks nice and the lines for the model traces also look good?

Thanks,

Paul

\documentclass[12pt]{article}
\usepackage[textwidth=6.25in]{geometry}
\usepackage{setspace}
\usepackage{here}
\usepackage[labelfont={bf}, justification=justified, singlelinecheck=false, labelsep=space]{caption}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{mathtools}

\tikzset{%
    ,>=latex
    ,trigl/.style={%
    ,isosceles triangle
    ,isosceles triangle apex angle=60
    ,draw
    ,shape border rotate=90
    ,inner sep=2
    ,font=\small\sffamily\bfseries
    ,minimum width=1.4cm % <== changed here
}
,circ/.style={%
    ,circle
    ,draw
    ,font=\small\sffamily\bfseries
    ,inner sep=6pt
    ,fill=white % <== changed here
}
,rect/.style={draw, font=\small\sffamily\bfseries, inner sep=9pt}
,input/.style={font=\small\sffamily\bfseries}
}

\newcommand*\onesub[1]{\strut$1\mathrlap{_{#1}}$} % <==  changed here
\newcommand*\onesubH[1]{\strut$H_{\mathrlap{1_{#1}}}$} % <== changed here
\newcommand*\onesubW[1]{\strut$W_{\mathrlap{1_{#1}}}$} % <== changed here

\newcommand\txtrect[1]{%
\scalebox{.8}{%
  \tikz[baseline=(c.base)]{
     \node [rect,inner sep=2.5pt, minimum height=0.7cm] (c) {#1};
  }%
 }%
}

\newcommand\txttrigl[1]{%
$\!$\scalebox{.6}{%
  \tikz[baseline=(c.base)]{
     \node[trigl,inner sep=1pt, minimum width=1.4cm] (c) {\onesub{#1}};
  }%
}$\!$%
}

\newcommand\txttriglH[1]{%
$\!$\scalebox{.6}{%
  \tikz[baseline=(c.base)]{
     \node[trigl,inner sep=1pt, minimum width=1.4cm] (c) {\onesubH{#1}};
  }%
}$\!$%
}

\newcommand\txttriglW[1]{%
$\!$\scalebox{.6}{%
  \tikz[baseline=(c.base)]{
     \node[trigl,inner sep=1pt, minimum width=1.4cm] (c) {\onesubW{#1}};
  }%
}$\!$%
}

\newcommand\txtcirc[1]{%
\scalebox{.7}{%}
  \tikz[baseline=-2.5pt]{
     \node [circ, inner sep=1pt] (c) {#1};
  }%
 }%
}

\begin{document}    
\begin{figure}[H]
    \caption{Growth Model using 3-Level Data}
    \label{lgm3lev}
        \begin{tikzpicture}[%
            ,auto
            ,every node/.append style={font=\scriptsize}
            ,every arrow/.style={line width=.5pt}
            ]

        \node[trigl]                    (12)   at (20.30,  4.95) {\onesub{2}};
        \node[rect]                     (AMj)  at (27.00,  2.00)    {$\mathit{CAgeMar}_{jk}$};

        \node[trigl, anchor=right side] (H11)  at (15.50,  0.75) {\onesubH{1}};
        \node[rect]                     (HTij) at (15.50, -2.60) {$\mathit{Hcyear}_{ij}$};
        \node[trigl, anchor=right side] (W11)  at (15.50, -5.90) {\onesubW{1}};
        \node[rect]                     (WTij) at (15.70, -8.75) {$\mathit{Wcyear}_{ij}$};
        \node[rect]                     (Yij)  at (27.00, -4.00) {$\textnormal{LifeSat}_{ij}$};

        \node[input]                    (M01k) at (17.25,-1.35) {$\mu_{01k}$};
        \node[input]                    (M11k) at (18.25, -4.30) {$\mu_{11k}$};
        \node[input]                    (M02k) at (20.25, -6.35) {$\mu_{02k}$};
        \node[input]                    (M12k) at (22.75, -7.35) {$\mu_{12k}$};
        \node[input]                    (rij)  at (30, -4.00) {$r_{ij}\begin{cases}r_{i1k}\\r_{i2k}\end{cases}$};

        \draw [->] (H11.right side)            to (Yij);
        \draw [->] (HTij.east)                 to (Yij);
        \draw [->] (W11.right side)            to (Yij);
        \draw [->] (WTij.east)                 to (Yij);

        \node[circ]                     (P01k) at (17.5, -0.05) {$\pi_{01k}$};
        \node[circ]                     (P11k) at (19.00, -2.90) {$\pi_{11k}$};
        \node[circ]                     (P02k) at (21.00, -5.00) {$\pi_{02k}$};
        \node[circ]                     (P12k) at (23.25, -5.75) {$\pi_{12k}$};         

        \draw [->] (12.255)                    to node [swap, near start] {$\beta_{010}$} (P01k);
        \draw [->] (12.265)                    to node [swap,pos=.35] {$\beta_{110}$} (P11k);
        \draw [->] (12.275)                    to node [swap,pos=.35] {$\beta_{020}$} (P02k);
        \draw [->] (12.285)                    to node [near start] {$\beta_{120}$} (P12k);

        \draw [->] (AMj.south)                 to node [swap, near start] {$\beta_{011}$} (P01k);
        \draw [->] (AMj.south)                 to node [swap, near start] {$\beta_{111}$} (P11k);
        \draw [->] (AMj.south)                 to node [swap, pos=.3] {$\beta_{021}$} (P02k);
        \draw [->] (AMj.south)                 to node [near start] {$\beta_{121}$} (P12k);

        \draw [->] (rij)                       to node[swap] {1} (Yij);

        \draw [->] (M01k)                      to node[near start]{1} (P01k);
        \draw [->] (M11k)                      to node[right,near start] {1} (P11k);
        \draw [->] (M02k)                      to node[right,near start] {1} (P02k);
        \draw [->] (M12k)                      to node[right,near start] {1} (P12k);

        \draw [<->, bend right=30]   ([xshift=-4mm]M01k.south east) to ([xshift=1mm]M11k.west);
        \draw [<->, bend right=30]   ([xshift=-4mm]M11k.south east) to (M02k);
        \draw [<->, bend right=30]   ([xshift=3mm]M02k.south west) to ([yshift=1mm]M12k.west);
        \draw [<->, bend right=45]   (M01k.south) to ([yshift=-1mm]M02k.west);
        \draw [<->, bend right=45]   ([xshift=3mm]M11k.south west) to (M12k.west);
        \draw [<->, bend right=65]   ([xshift=-1mm]M01k.south) to ([yshift=-1mm]M12k.west);
        \draw [<->, bend left]       ([xshift=-3mm, yshift=-2mm]rij.north east) to ([xshift=-3mm, yshift=1mm]rij.south east);
    \end{tikzpicture}
\end{figure}

\begin{onehalfspace}
% \begin{doublespace}

\section{Steps for Deriving the Reduced Form Equation}

Below is a summary showing the derivation of the reduced form
equation.

\hspace{9pt} Traces for Fixed Effects:

\hspace{24pt} \txttrigl{2} $\times \beta_{010} \times$ \txttriglH{1} = $\beta_{010}$

\hspace{24pt} \txttrigl{2} $\times \beta_{110} \times$ \txtrect{$Hcyear_{ijk}$} = $\beta_{110} Hcyear_{ijk}$

\hspace{24pt} \txttrigl{2} $\times \beta_{020} \times$ \txttriglW{1} = $\beta_{020}$

\hspace{24pt} etc \ldots

\section{Steps for Deriving the Multilevel Equations}

Below is a summary showing the derivation of the multilevel equations.

\hspace{8pt} Level 1:

\hspace{24pt} \txtrect{$LifeSat_{ij}$} 

\hspace{40pt} Traces:

\hspace{56pt} \txttriglH{1} $\times$ \txtcirc{$\pi_{01k}$} = $\pi_{01k}$

\hspace{56pt} \txtrect{$Hcyear_{ijk}$} $\times$ \txtcirc{$\pi_{11k}$} = $\pi_{11k} Hcyear_{ijk}$

\hspace{24pt} etc \ldots

\end{onehalfspace}
% \end{doublespace}

\end{document}

Would appreciate some help formatting a TikZ figure. I've included my code for creating the figure below.

The main trouble I'm having is with the triangles labeled H11 and W11. I like the way the triangle labeled 12 looks and would like the others to be a little more like it. My feeling is that I'd like the "H" in H11 and the "W" in W11 to be more-or-less centered in the triangle like the "1" in 12. It may be though that there's too much text in H11 and W11 for this to work without making the triangles quite large. At any rate, the triangles for H11 and W11 are larger than the one for 12 and my sense is that the result is not esthetically pleasing. Maybe the best thing to do is to make the triangle for 12 bigger so it's more consistent with the other ones. I'm not totally sure what the solution is. I just know that something doesn't quite look right here.

Another thing I wondered is if it's possible to improve the construction of the lines running "through" the circles in the figure. I've constructed these using two separate lines. One line runs from a triangle or box on the left of the figure to the circle. The other then runs from the circle to the box labeled "LifeSat" on the right of the figure. The downside to this is that the two-part arrows tend not to be truly straight. I imagine it might be possible to draw a single line here that would be truly straight but am not sure how to do it.

Other suggestions about how to improve the code also would be welcome.

Thanks,

Paul

\documentclass[12pt]{article}

%%%% Packages %%%%

\usepackage[textwidth=6.25in]{geometry}
\usepackage{here}
\usepackage[labelfont={bf}, justification=justified, singlelinecheck=false, labelsep=space]{caption}
\usepackage{tikz}
\usetikzlibrary{calc,shapes,shapes.geometric,positioning}
\usepackage[
pdftex,
pdfstartpage=1,
pdfpagemode=UseOutlines,
bookmarks,
bookmarksopen,
pdfstartview=Fit,
pdfview=Fit,
colorlinks,
linktocpage,
linkcolor=blue,
citecolor=blue,
pagebackref=true]
{hyperref}

%%%% Set pdf zoom to 100% %%%%

\hypersetup{pdfstartview={XYZ null null 1.00}, pdfview={XYZ null null 1.00}}

%%%% TikZ graphics styles/commands %%%%

\tikzstyle{arr}=[-latex, black, line width=0.5pt]
\tikzstyle{doublearr}=[latex-latex, black, line width=0.5pt]
\tikzstyle{input}=[font=\small\sffamily\bfseries]
\tikzstyle{rect}=[rectangle, draw=black, font=\small\sffamily\bfseries, inner sep=9pt]
\tikzstyle{circ}=[circle, draw=black, font=\small\sffamily\bfseries, inner sep=6pt]
\tikzstyle{trigl}=[
isosceles triangle,  
draw, 
shape border rotate=90, 
inner sep=2, 
font=\small\sffamily\bfseries,
isosceles triangle apex angle=60,
isosceles triangle stretches
]

\def\onesub#1{\strut$1\rlap{$_{#1}$}$}
\def\onesubH#1{\strut$H_{1\rlap{$_{#1}$}}$}
\def\onesubW#1{\strut$W_{1\rlap{$_{#1}$}}$}

\newcommand\txtrect[1]{%
\scalebox{.8}{%
  \tikz[baseline=(c.base)]{
  \node [rect,inner sep=2.5pt] (c) {#1};  
  }%
 }%
}

\newcommand\txttrigl[1]{%
$\!$\scalebox{.6}{%
  \tikz[baseline=(c.base)]{
     \node[trigl,inner sep=1pt] (c) {\onesub{#1}};
  }%
}$\!$%
}

\newcommand\txttriglH[1]{%
$\!$\scalebox{.6}{%
  \tikz[baseline=(c.base)]{
     \node[trigl,inner sep=1pt] (c) {\onesubH{#1}};
  }%
}$\!$%
}

\newcommand\txttriglW[1]{%
$\!$\scalebox{.6}{%
  \tikz[baseline=(c.base)]{
     \node[trigl,inner sep=1pt] (c) {\onesubW{#1}};
  }%
}$\!$%
}

\newcommand\txtcirc[1]{%
\scalebox{.7}{%}
  \tikz[baseline=-2.5pt]{
     \node [circ, inner sep=1pt] (c) {#1};
  }%
 }%
}

%%%% Main document %%%%

\begin{document}

{
\small

\newcommand{\rlist}{
\left\{
\begin{array}{cl}
r_{i1k} \\ 
r_{i2k} 
\end{array}
\right.
}

\begin{figure}[H]
\caption{Growth Model using 3-Level Data}
\label{lgm3lev}
\begin{tikzpicture}[auto]

\node[trigl]                    (12)   at (20.30,  4.95) {\onesub{2}};
\node[rect]                     (AMj)  at (27.00,  2.00)    {$\mathit{CAgeMar}_{jk}$};

\node[trigl, anchor=right side] (H11)  at (15.50,  0.75) {\onesubH{1}};
\node[rect]                     (HTij) at (15.50, -2.60) {$\mathit{Hcyear}_{ij}$};
\node[trigl, anchor=right side] (W11)  at (15.50, -5.90) {\onesubW{1}};
\node[rect]                     (WTij) at (15.70, -8.75) {$\mathit{Wcyear}_{ij}$};
\node[rect]                     (Yij)  at (27.00, -4.00) {$LifeSat_{ij}$};

\node[circ]                     (P01k) at (17.5, 0) {$\pi_{01k}$};
\node[circ]                     (P11k) at (19.00, -2.90) {$\pi_{11k}$};
\node[circ]                     (P02k) at (21.00, -5.00) {$\pi_{02k}$};
\node[circ]                     (P12k) at (23.25, -6.00) {$\pi_{12k}$};

\node[input]                    (M01k) at (17.25,-1.35) {$\mu_{01k}$};
\node[input]                    (M11k) at (18.25, -4.30) {$\mu_{11k}$};
\node[input]                    (M02k) at (20.25, -6.35) {$\mu_{02k}$};
\node[input]                    (M12k) at (22.75, -7.35) {$\mu_{12k}$};
\node[input]                    (rij)  at (30, -4.00) {\small$r_{ij} \rlist$};

\draw [arr] ([xshift=-2mm]12.south)    to node [pos=.20, swap, xshift=2mm, yshift=0mm] {\scriptsize$\beta_{010}$} (P01k);
\draw [arr] ([xshift=-0.75mm]12.south) to node [pos=.20, swap, xshift=0.75mm, yshift=-5mm] {\scriptsize$\beta_{110}$} (P11k);
\draw [arr] ([xshift=0.75mm]12.south)  to node [pos=.20, swap, xshift=2mm, yshift=-5mm] {\scriptsize$\beta_{020}$} (P02k);
\draw [arr] ([xshift=2mm]12.south)     to node [pos=.20, xshift=-1mm, yshift=0mm] {\scriptsize$\beta_{120}$} (P12k);

\draw [arr] (AMj.south)                to node [pos=.20, swap, xshift=-2mm, yshift=-1mm] {\scriptsize$\beta_{011}$} (P01k);
\draw [arr] (AMj.south)                to node [pos=.20, swap, xshift=-2mm, yshift=-3mm] {\scriptsize$\beta_{111}$} (P11k);
\draw [arr] (AMj.south)                to node [pos=.20, swap, xshift=-2mm, yshift=-5mm] {\scriptsize$\beta_{021}$} (P02k);
\draw [arr] (AMj.south)                to node [pos=.20, xshift=-2mm, yshift=0mm] {\scriptsize$\beta_{121}$} (P12k);

\draw (H11.right side)                 to (P01k);
\draw [arr] (P01k)                     to (Yij);

\draw (HTij.east)                      to (P11k);
\draw [arr] (P11k)                     to (Yij);

\draw (W11.right side)                 to (P02k);
\draw [arr] (P02k)                     to (Yij);

\draw (WTij.east)                      to (P12k);
\draw [arr] (P12k)                     to (Yij);

\draw [arr] (rij)                      to node[swap] {\scriptsize 1} (Yij);

\draw [arr] (M01k)                     to node[pos=.33, xshift=1mm, yshift=-1.5mm] {\scriptsize 1} (P01k);
\draw [arr] (M11k)                     to node[swap, xshift=-1mm, yshift=1.5mm] {\scriptsize 1} (P11k);
\draw [arr] (M02k)                     to node[pos=.33, swap, xshift=-1mm, yshift=2mm] {\scriptsize 1} (P02k);
\draw [arr] (M12k)                     to node[swap, xshift=-1mm, yshift=1.5mm] {\scriptsize 1} (P12k);

\draw [doublearr, bend right=30]   ([xshift=-4mm]M01k.south east) to ([xshift=1mm]M11k.west);
\draw [doublearr, bend right=30]   ([xshift=-4mm]M11k.south east) to (M02k);
\draw [doublearr, bend right=30]   ([xshift=3mm]M02k.south west) to ([yshift=1mm]M12k.west);
\draw [doublearr, bend right=45]   (M01k.south) to ([yshift=-1mm]M02k.west);
\draw [doublearr, bend right=45]   ([xshift=3mm]M11k.south west) to (M12k.west);
\draw [doublearr, bend right=65]   ([xshift=-1mm]M01k.south) to ([yshift=-1mm]M12k.west);
\draw [doublearr, bend left]       ([xshift=-1mm, yshift=-2mm]rij.north east) to ([xshift=-1mm, yshift=1mm]rij.south east);

\end{tikzpicture}
\end{figure}
}

\end{document}

1 Answer 1

8

You should have a look on the library positioning and the chapter graph drawing of the pgf manual. Right now, your are defining each node by its coordinates which is a lot of hard coding. Not very flexible.

Here is something to start with:

% arara: pdflatex

\documentclass[12pt]{article}
\usepackage[textwidth=6.25in]{geometry}
\usepackage{here}
\usepackage[labelfont={bf}, justification=justified, singlelinecheck=false, labelsep=space]{caption}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{mathtools}

\tikzset{%
    ,>=latex
    ,trigl/.style={%
        ,isosceles triangle
        ,isosceles triangle apex angle=60
        ,draw
        ,shape border rotate=90
        ,inner sep=2
        ,font=\small\sffamily\bfseries
        ,minimum width=1.4cm % <== changed here
    }
    ,circ/.style={%
        ,circle
        ,draw
        ,font=\small\sffamily\bfseries
        ,inner sep=6pt
        ,fill=white % <== changed here
    }
    ,rect/.style={draw, font=\small\sffamily\bfseries, inner sep=9pt}
    ,input/.style={font=\small\sffamily\bfseries}
    }

\newcommand*\onesub[1]{\strut$1\mathrlap{_{#1}}$} % <==  changed here
\newcommand*\onesubH[1]{\strut$H_{\mathrlap{1_{#1}}}$} % <== changed here
\newcommand*\onesubW[1]{\strut$W_{\mathrlap{1_{#1}}}$} % <== changed here

\begin{document}    
\begin{figure}[H]
    \caption{Growth Model using 3-Level Data}
    \label{lgm3lev}
        \begin{tikzpicture}[%
                ,auto
                ,every node/.append style={font=\scriptsize}
                ,every arrow/.style={line width=.5pt}
                ]

            \node[trigl]                    (12)   at (20.30,  4.95) {\onesub{2}};
            \node[rect]                     (AMj)  at (27.00,  2.00)    {$\mathit{CAgeMar}_{jk}$};

            \node[trigl, anchor=right side] (H11)  at (15.50,  0.75) {\onesubH{1}};
            \node[rect]                     (HTij) at (15.50, -2.60) {$\mathit{Hcyear}_{ij}$};
            \node[trigl, anchor=right side] (W11)  at (15.50, -5.90) {\onesubW{1}};
            \node[rect]                     (WTij) at (15.70, -8.75) {$\mathit{Wcyear}_{ij}$};
            \node[rect]                     (Yij)  at (27.00, -4.00) {$\textnormal{LifeSat}_{ij}$};

            \node[input]                    (M01k) at (17.25,-1.35) {$\mu_{01k}$};
            \node[input]                    (M11k) at (18.25, -4.30) {$\mu_{11k}$};
            \node[input]                    (M02k) at (20.25, -6.35) {$\mu_{02k}$};
            \node[input]                    (M12k) at (22.75, -7.35) {$\mu_{12k}$};
            \node[input]                    (rij)  at (30, -4.00) {$r_{ij}\begin{cases}r_{i1k}\\r_{i2k}\end{cases}$};

            \draw [->] (H11.right side)                 to (Yij);
            \draw [->] (HTij.east)                      to (Yij);
            \draw [->] (W11.right side)                 to (Yij);
            \draw [->] (WTij.east)                      to (Yij);

            \node[circ]                     (P01k) at (17.5, 0) {$\pi_{01k}$};
            \node[circ]                     (P11k) at (19.00, -2.90) {$\pi_{11k}$};
            \node[circ]                     (P02k) at (21.00, -5.00) {$\pi_{02k}$};
            \node[circ]                     (P12k) at (23.25, -6.00) {$\pi_{12k}$};         

            \draw [->] (12.255)    to node [swap, near start] {$\beta_{010}$} (P01k);
            \draw [->] (12.265) to node [swap,pos=.35] {$\beta_{110}$} (P11k);
            \draw [->] (12.275)  to node [swap,pos=.35] {$\beta_{020}$} (P02k);
            \draw [->] (12.285)     to node [near start] {$\beta_{120}$} (P12k);

            \draw [->] (AMj.south)                to node [swap, near start] {$\beta_{011}$} (P01k);
            \draw [->] (AMj.south)                to node [swap, near start] {$\beta_{111}$} (P11k);
            \draw [->] (AMj.south)                to node [swap, pos=.3] {$\beta_{021}$} (P02k);
            \draw [->] (AMj.south)                to node [near start] {$\beta_{121}$} (P12k);

            \draw [->] (rij)                      to node[swap] {1} (Yij);

            \draw [->] (M01k)                     to node[near start]{1} (P01k);
            \draw [->] (M11k)                     to node[right,near start] {1} (P11k);
            \draw [->] (M02k)                     to node[right,near start] {1} (P02k);
            \draw [->] (M12k)                     to node[right,near start] {1} (P12k);

            \draw [<->, bend right=30]   ([xshift=-4mm]M01k.south east) to ([xshift=1mm]M11k.west);
            \draw [<->, bend right=30]   ([xshift=-4mm]M11k.south east) to (M02k);
            \draw [<->, bend right=30]   ([xshift=3mm]M02k.south west) to ([yshift=1mm]M12k.west);
            \draw [<->, bend right=45]   (M01k.south) to ([yshift=-1mm]M02k.west);
            \draw [<->, bend right=45]   ([xshift=3mm]M11k.south west) to (M12k.west);
            \draw [<->, bend right=65]   ([xshift=-1mm]M01k.south) to ([yshift=-1mm]M12k.west);
            \draw [<->, bend left]       ([xshift=-3mm, yshift=-2mm]rij.north east) to ([xshift=-3mm, yshift=1mm]rij.south east);
    \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .