3

I made a picture using tikz, and I would like to correctly align the captions. This is what I did:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\pgfmathparse{sqrt(12)}
\coordinate (C) at (-2,\pgfmathresult);
\coordinate (A) at (-4,0);
\coordinate (B) at (4,0);
\coordinate (H) at (-2,0);
\draw[color=blue, line width=2pt] (A) -- (H);

\draw [fill=blue] (A) circle (2.0pt);
\draw[anchor=north] (A) node {$A$};

\draw [fill=blue] (B) circle (2.0pt);
\draw[anchor=north] (B) node {$B$};

\draw [fill=blue] (H) circle (2.0pt);
\draw[anchor=north] (H) node {$H$};

\draw [fill=blue] (C) circle (2.0pt);
\draw[anchor=south] (C) node {$C$};

\draw[anchor=north] (-3,0) node {$1$};
\draw[anchor=north] (1,0) node {$x$};
\draw[anchor=west] (-2,\pgfmathresult/2) node {$\sqrt{x}$};

\draw[color=blue, line width=2pt] (H) -- (B);
\draw[color=blue, line width=2pt] (B) arc (0:180:4);
\draw[color=red, line width=2pt] (H) -- (C);
\draw[color=purple, line width=2pt] (A) -- (C) -- (B);
\end{tikzpicture}
\end{document}

And this is the output:

enter image description here

As you can see, H, B and x are not properly aligned: x is misplaced.

Which is the correct way to align them?

  • 1
    A, 1, H, x` and B aren't captions but node and edge labels :-). for start for x try to write x\vphantom{X}. – Zarko Jul 7 '18 at 12:20
  • 2
    Use text height=... and text deph=... as node options. – CarLaTeX Jul 7 '18 at 12:41
4

i suppose that you like like this:

enter image description here

i took a liberty and with use of the library quotes make your code quite shorter:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{quotes}

\begin{document}
    \begin{tikzpicture}[
    line cap=round,
    auto=right,
    label distance=0.5ex,
    dot/.style={circle,fill=blue,inner sep=2pt,outer sep=0pt},
every edge quotes/.append style={text=black, text height=2ex}
                        ]
\pgfmathparse{sqrt(12)}
\coordinate[label=$C$]       (C) at (-2,\pgfmathresult);
\coordinate[label=below:$A$] (A) at (-4,0);
\coordinate[label=below:$B$] (B) at ( 4,0);
\coordinate[label=below:$H$] (H) at (-2,0);
%
\foreach \i in {A, B, C, H}
\node[dot] at (\i) {};
    \begin{scope}[line width=2pt]
\draw[color=blue]   (A) to ["1"] (H)
                        to ["$x$"] (B) arc(0:180:4);
\draw[color=red]    (H) to ["$\sqrt{x}$"] (C);
\draw[color=purple] (A) -- (C) -- (B);
    \end{scope}
\end{tikzpicture}
\end{document}
  • Thank you, this code is very elegant. Where exactly did you solve my problem? It is just the line "label distance=0.5ex"? – zar Jul 7 '18 at 13:31
  • 1
    with every edge quotes/.append style={text=black, text height=2ex} where text height=2ex reserve space for text in edge quote (label). with label distance=0.5ex are defined distance of coordinates labels so, that they are aligned with edges. – Zarko Jul 7 '18 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.