2

I have read the related question: Tikz Understand white space between node and draw, and tried the solutions proposed therein.

I am trying to draw a phylogenetic tree. I define the leaves and internal node as tikz's \node, and connect them with \draw.

However, I can't seem to get rid of the gap between line segments in the drawn picture:

Here's what I'm writing to generate the above diagram:

\begin{tikzpicture}
  \tikzstyle{internal}=[inner sep=0, outer sep=0, line cap=rect]
  \node (C) at (0, 0) {$C$};
  \node (H) at (1, 0) {$H$};
  \node[internal] (CH) at (0.5, 0.5) {};
  \draw (C) -- (CH) -- (H);
\end{tikzpicture}

It includes the inner sep=0, outer sep=0, and line cap=rect options recommended in a similar question (linked above).

4
  • 5
    \coordinate (CH) at (0.5, 0.5);?
    – user194703
    Oct 8, 2019 at 8:38
  • 1
    Use of \tikzstyle is discouraged. One should use \tikzset{internal/.style={inner sep=0, outer sep=0, line cap=rect}} instead. Though of course, a coordinate would be the proper solution in this case. And that could be done in the optional argument of tikzpicture: \begin{tikzpicture}[internal/.style={...}]
    – Skillmon
    Oct 8, 2019 at 8:39
  • @Schrödinger'scat You're 100% right, I should have used \coordinate. My bad!
    – ning
    Oct 8, 2019 at 10:28
  • If you are drawing multiple trees, you should consider using one of the libraries or packages designed for this.
    – cfr
    Oct 8, 2019 at 22:21

2 Answers 2

3

You can do it in a single line like this:

\documentclass[tikz, border=2pt]{standalone}

\begin{document}

\begin{tikzpicture}[inner sep=2pt]
  \draw (0,0) node [below left]{$C$} -- ++(.5,.5) -- ++(.5,-.5) node[below right]{$H$};   
\end{tikzpicture}

\end{document}

enter image description here

Addendum:

It is easy to also keep coordinates' names for later reference using:

\draw (0,0) coordinate[label={below left:$C$}](C) -- ++(.5,.5)coordinate(CH) -- ++(.5,-.5) coordinate[label={below right:$H$}](H);
1

Schrödinger's cat was right. I should be using coordinates instead of nodes for the internal nodes of the tree. This is my preferred solution over AboAmmar's ++ syntax since I want to save the coordinate of the internal node for use later on.

1
  • ,@ning -- TikZ is powerful enough that you can still keep coordinates' names for later reference with a small modification on my code (see addendum). Relative coordinates are usually easier to use and modify, IMO.
    – AboAmmar
    Oct 20, 2019 at 14:37

You must log in to answer this question.

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