3

I have a box and two additional lines. Now I want to add some text horizontally aligned to the lines halfway. I did not find any useful examples which were not too complicated.

Another problem I am trying to solve is that I want to connect the vertical line with the box. Why is there a small distance? How can I get rid of it? And why do the two lines not connect in the middle of the box? How can I solve this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
\node at (-4,3) (nodeXi) {$x_i$};
\node at (2,2) (nodeXj) {$x_j$};
\node at (2,4) (nodeD) {};
\draw (nodeXi) -- (nodeXj);
\draw (nodeXj) -- (nodeD);
\end{tikzpicture}
\end{center}
\caption{A meaningful caption}
\end{figure}
\end{document}

enter image description here

2

2 Answers 2

3

When you create a node, it has a rectangular shape by default and a positive inner sep which pads the contents. Even if the node is empty, it therefore has size.

When you draw from or to a node, TikZ calculates the nearest point on the node's border and draws to or from that. You can see what is going on by adding a drawing command to all the nodes in the picture:

drawn nodes

There are various ways to avoid this effect when it is unwanted. One is to specify that the node have the coordinate shape so that it has no size.

\node [coordinate] ...;
\coordinate ...;

Another is to specify the anchor you want the drawing command to begin or end at.

\draw ... (<node>.<anchor>) ...;

For example,

  \draw (nodeXi.center) -- (nodeXj.center);
  \draw (nodeXj.center) -- (nodeD.center);

coordinates and nodes' centres

\documentclass[border=10pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[every node/.append style={draw=red}]
  \draw (0,0) rectangle (4,4);
  \node at (-4,3) (nodeXi) {$x_i$};
  \node at (2,2) (nodeXj) {$x_j$};
  \node at (2,4) (nodeD) {};
  \draw (nodeXi.center) -- (nodeXj.center);
  \draw (nodeXj.center) -- (nodeD.center);
  \begin{scope}[yshift=50mm]
    \draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
    \coordinate [label=below:$x_i$] (nodeXi') at (-4,3);
    \coordinate [label=below:$x_j$] (nodeXj') at (2,2);
    \node [coordinate] at (2,4) (nodeD') {};
    \draw (nodeXi') -- (nodeXj');
    \draw (nodeXj') -- (nodeD');
  \end{scope}
\end{tikzpicture}
\end{document}

If I understood the other question, I'd try to answer it. But I don't, so I can't.

2
  • you start new year very hard working (+1)! Happy New Year!
    – Zarko
    Jan 1, 2017 at 2:56
  • Happy New Year to you also, @Zarko.
    – cfr
    Jan 1, 2017 at 4:23
3

It is not very clear what you mean with "... text horizontally aligned to the lines halfway ...". See if the following solution is what you're looking for:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\node (box) [draw, minimum size=4cm] at (2,2) {};
\node at (-4,3) (nodeXi) {$x_i$};
\node at (box)  (nodeXj) {$x_j$};
\draw (nodeXi) -- node[above=3mm] {horizontal text} (nodeXj);
\draw (nodeXj) -- node[right] {horizontal text}  (box.north);
\end{tikzpicture}
\end{center}
\caption{A meaningful caption}
\end{figure}
\end{document}

Addendum: according to your comment, I guess that you'd like to obtain something like this:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\node (box) [draw, minimum size=4cm] at (2,2) {};
\node  (nodeXi) at (-4,3){$x_i$};
\draw (nodeXi)  -- node[above=3mm] {horizontal text} (box.center) 
                   node[right] {$x_j$}  
                -- node[right] {horizontal text}  (box.north);
\end{tikzpicture}
\end{center}
\caption{A meaningful caption}
\end{figure}
\end{document}
3
  • Is there a way to connect the two lines in the middle of the box?
    – Gilfoyle
    Jan 1, 2017 at 1:49
  • of course, but where than you like to have a label of node? Happy New Year!
    – Zarko
    Jan 1, 2017 at 2:04
  • Just near the connection. Somewhere on the right would be great.
    – Gilfoyle
    Jan 1, 2017 at 2:28

You must log in to answer this question.

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