[Tex/LaTex] Simple tikz picture, a line connecting nodes

tikz-pgf

I would like to align the text a — b — c at the bottom and not at the top as it is now:

\documentclass{article}
\usepackage{pgfplots,tikz}
\usetikzlibrary{automata,positioning}

\begin{document}
\begin{tikzpicture} 
\node(a) {a}; 
\node[right=of a] (b) {b}; 
\node[right=of b] (c) {c}; 
\draw (a) -- (b) -- (c);  
\end{tikzpicture}

\end{document}

Moreover, I would like to know: Do I have to place the tikzpicture always inside a "figure" environment? Because, if I do not, then it appears aligned with the rest of the text and not as "a picture".
Lastly, I would like to have small circles in the place of a, b, c in the tikzpicture above (and the names a,b,c above these circles), but when I do it, the connecting lines touch the circles, which I do not like. I would prefer the current format (where the line does not touch a, b and c but connects them) and have circles instead. How can I do it?

Since it is just a simple picture of a line connecting three points, I would like to keep the code as simple and basic as possible. Thanks.

Best Answer

In your original code, to align the nodes at the bottom, you can set the appropriate anchors, by saying right=of a.south east,anchor=south west, instead of just right=of a. Depending on what you want, you might want to use the base west anchor instead of south west, but then you can use base right=of instead of right=of, as shown in the code below.

You do get problems with drawing the lines, as they appear to be drawn between the east and west anchors, and since these are at different vertical positions, the lines are tilted a bit. There are probably several methods for fixing that, the one shown below is like this:

\draw (a) -- (b.west |- a.east)
      (c.west |- a.west) -- (a.west -| b.east);

The construct (<coord 1> |- <coord 2>) is a coordinate specification (see section 13.3.1, Intersections of perpendicular lines, in the manual). It is the point that has the x-coordinate of <coord 1>, and the y-coordinate of <coord 2>. Think of |- as two lines, one going vertically from the first point, one horizontally from the second, and the coordinate is at the intersection of those. Similarly, with -| you get the y-coordinate of the first point, and the x-coordinate of the second.

Note further that if you do \draw (a) -- (b) (c) -- (d); then you will not get a line between b and c, because there is no -- between the coordinates.

Finally, I show a method for having circles with letters above. If you don't require the vertical alignment at the baseline, you can remove the [anchor=base,label distance=1mm] from the circ style.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
Aligning nodes at their bottom (left) or baseline (right):

\begin{tikzpicture} 
\node(a) {a}; 
\node[right=of a.south east,anchor=south west] (b) {b}; 
\node[right=of b.south east,anchor=south west] (c) {y}; 
\draw (a) -- (b) -- (c);  
\end{tikzpicture}
\begin{tikzpicture} 
\node(a) {a}; 
\node[base right=of a] (b) {b}; 
\node[base right=of b] (c) {y}; 
\draw (a) -- (b) -- (c);  
\end{tikzpicture}

Tweaked to get horizontal lines:

\begin{tikzpicture} 
\node(a) {a}; 
\node[right=of a.south east,anchor=south west] (b) {b}; 
\node[right=of b.south east,anchor=south west] (c) {y}; 
\draw (a) -- (b.west |- a.east)
      (c) -- (c.west -| b.east);  
\end{tikzpicture}
\begin{tikzpicture} 
\node(a) {a}; 
\node[base right=of a] (b) {b}; 
\node[base right=of b] (c) {y}; 
\draw (a) -- (b.west |- a.east)
      (c.west |- a.west) -- (a.west -| b.east);  
\end{tikzpicture}

With circles:

\begin{tikzpicture}[
   circ/.style={ % style for the nodes
     draw=blue!50,  % draw with given color
     circle,
     fill=blue!10, % fill color
     inner sep=0pt, % remove all padding in node
     minimum size=2mm, % set size
     outer sep=2pt, 
     label={[anchor=base,label distance=1mm]above:#1} % The #1 indicates that the style takes one argument, so use as circ={...}
    }]

\node [circ=a] (a) {}; 
\node [circ=b, right=of a] (b) {}; 
\node [circ=y, right=of b] (c) {}; 
\draw (a) -- (b) -- (c);  
\end{tikzpicture}
\end{document}

Addendum -- mid

After glancing at the manual I realized that the mid east/mid west anchors can be used here along with base right for slightly shorter code, at least for the present case. The mid anchors are defined to be 0.5ex above the baseline.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture} 
\node(a) {a}; 
\node[base right=of a] (b) {b}; 
\node[base right=of b] (c) {y}; 
\draw (a.mid east) -- (b.mid west) (b.mid east) -- (c.mid west);  
\end{tikzpicture}
\end{document}