我使用 tikz 制作了一张图片,我想正确对齐标题。 这是我所做的:
\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}
输出结果如下:
如你看到的,H,乙和X没有正确对齐:X放错了地方。
哪种方法才是正确的对齐方式?
答案1
我想你喜欢这样:
我擅自使用该库quotes
使您的代码变得更短:
\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}