Graphics, Figures & TablesNeed help polishing a TikZ figure

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Need help polishing a TikZ figure

Post by pjmiller_57 »

Hello All,

Need help polishing a figure I've drawn using TikZ. Let me begin by saying that I think TikZ is wonderful. I tried drawing figures like this using Microsoft Word previously. Using Word was quite painful. In contrast, learning to draw using TikZ was actually quite fun.

The code for drawing the figure appears below. I quite like the figure. I just need to make a couple of minor changes. In each of the triangles, I want the text centered both horizontally and vertically. Also, I'd like the line from the triangle on the far left to emanate from the middle of the right side. At the moment, it is placed to close to the bottom of the triangle.

The other thing I've been trying to figure out is how to refer to the components of the diagram in text. So, for example, I've figured out how to draw a circle in text, as shown immediately below the figure. What I'd really like to have appear there though is a triangle with a 1 and a subscripted 1 inside, like in the far left of the figure. The same goes for for the eleents with a 1 followed by a subscript under "Fixed Effects".

If anyone could show me how to make the necessary changes, I'd be most greatful.

Thanks,

Paul

Code: Select all

\documentclass{article}
\setlength{\textwidth}{6.25in}
\setlength{\oddsidemargin}{0in}
\usepackage{parskip}
\usepackage{setspace}
\usepackage{here}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric}
\usepackage[pdfview=Fit]{hyperref}

\begin{document}

\tikzstyle{arr}=[-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=3, 
   font=\small\sffamily\bfseries,
   isosceles triangle apex angle=60,
   isosceles triangle stretches]

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[auto]
% \node[trigl]  (11)  at (16, -0.23) {$1_1$};
\node[trigl]  (11)  at (16, 0) {$1_1$};
\node[circ]   (B0j) at (20, 0) {$\beta_{oj}$};
\node[trigl]  (12)  at (20, 3) {$1_2$};
\node[rect]   (Yij) at (24, 0) {$Y_{ij}$};
\node [input] (M0j) at (20,-3) {$\mu_{0j}$};
\node [input] (rij) at (26, 0) {$r_{ij}$};

% \draw (11.right side)        to (B0j);
\draw (11)        to (B0j);
\draw [arr] (12)  to node {\scriptsize$\gamma_{00}$} (B0j);
\draw [arr] (B0j) to (Yij);
\draw [arr] (M0j) to (B0j);
\draw [arr] (rij) to (Yij);
\end{tikzpicture}
\end{center}
\caption{The Unconditional Means Model}
\end{figure}

Note how components from the figure (e.g., \tikz \draw (0,0) circle
(5pt);) can easily be inserted into inline text.

\begin{doublespace}

Fixed Effects 

\hspace{6pt} Trace from $1_2$
  
\hspace{18pt} $(1_2$ $\gamma_{00}$ $1_1)$ 

Random Effects 

\hspace{18pt} $(\mu_{0j}) + (r_{0j})$

Multilevel Equations 

\begin{tabbing}
\hspace*{0.75in} \= ft \= \hspace*{3in} \= st \kill

\hspace{6pt} Level 1 \>\> $(1)(\beta_{oj}); (r_{ij})(1)$ \\
\>\> $Y_{ij} = \beta_{oj} + r_{ij}$ \\

\hspace{6pt} Level 2 \>\> $(1)(\gamma_{00}); (\mu_{0j})(1)$ \\
\>\> $\beta_{oj} = \gamma_{00} + \mu_{0j}$

\end{tabbing}

Reduced Form

\hspace{6pt} $Y_{ij} = [\gamma_{00}] + [\mu_{0j} + r_{0j}]$

\end{doublespace}

\end{document}
Last edited by Stefan Kottwitz on Mon Nov 12, 2012 4:33 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Need help polishing a TikZ figure

Post by Stefan Kottwitz »

Hi Paul!

Such triangles have their anchor at a lower position. You could change that anchor and refer to it, such as:

Code: Select all

\node[trigl, anchor=right side]  (11)  at (16, 0) {$1_1$};
\draw (11.right side) to (B0j);
drawing.png
drawing.png (4.06 KiB) Viewed 7065 times
The anchor could be more generally defined in the trig 1 style, just the straightforward way above was easier to do.
However, the node text ist not aligned with other node texts then, perhaps you need to adjust the base of the node.

Regarding the second problem, you could also draw nodes in the text. Here it's good to give the node a name and refere to it's baseline for aligning the node text with the outer text.

Code: Select all

Note how components from the figure (e.g.,
\tikz[baseline=(trig.base)]\node (trig) [trigl] {$1_1$};)
can easily be inserted into inline text
text.png
text.png (3.88 KiB) Viewed 7065 times
Stefan
LaTeX.org admin
pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Re: Need help polishing a TikZ figure

Post by pjmiller_57 »

Hi Stefan,

Thanks for your reply. Very helpful and brought me at least a couple of steps closer. Still need to center the text in the triangles horizontally and vertically. Might also want to control how large the elements from the figure are in inline text. I don't quite know how to do that yet, but your your reply seems to offer some helpful hints. I'll try working on this a bit more tomorrow, as I've no time left to continue working on it today.

Thanks again.

Paul
pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Need help polishing a TikZ figure

Post by pjmiller_57 »

Hi Stefan,

Looked around a bit and found some minor adjusts. These may not be the best way of doing things but they do give me the result I need.

To raise the text in the triangles, I added text depth=1ex to the trigl style.

To control the size/appearance of the triangles in inline text, I used:

Code: Select all

\newcommand\txttrigl{\tikz[baseline=(trig.base)]\node (trig) [trigl,inner sep=0.5pt]}
Note how components from the figure (e.g., \txttrigl {$1_1$};) can
easily be inserted into inline text.

Thanks again for your help with changing the anchor on one of the triangles.

Paul
Last edited by Stefan Kottwitz on Sat Nov 17, 2012 3:39 pm, edited 1 time in total.
Post Reply