1

I want to put the annotation text (x and y) at the exact middle.current example has a big offset due to the line slopped.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\begin{document}
\begin{tikzpicture}
\newcommand{\annL}[6] [] {
    \coordinate (AE1) at ($(#2)!#4!90:(#3)$);
    \coordinate (BE1) at ($(#3)!#4!-90:(#2)$);
    \coordinate (AE2) at ($(#2)!#5!90:(#3)$);
    \coordinate (BE2) at ($(#3)!#5!-90:(#2)$);
    \draw[>=latex,red,<->]  (AE2) -- (BE2) node[midway,sloped,auto,align=center] {#6};
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#2) -- (AE1);
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#3) -- (BE1);
}
\newcommand{\annC}[6] [] {
   \coordinate (AE1) at ($(#2)!#4!90:(#3)$);
    \coordinate (BE1) at ($(#3)!#4!-90:(#2)$);
   \draw [decorate,decoration={brace,amplitude=#5},xshift=0pt,yshift=0pt]
          (AE1) -- (BE1) node [black,midway,sloped,auto,yshift=#5]  {#6} ;
}
\coordinate (A) at (0,0);
\coordinate (B) at (1,2);
\draw (A) -- (B);
\annC{A}{B}{1mm}{0.5mm}{x};
\annL{B}{A}{5mm}{4mm}{y};
\end{tikzpicture}
\end{document}

Output: enter image description here

3

The problem is the auto placement of nodes you request. Just chose explicitly above or below and it should be fine.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\begin{document}
\begin{tikzpicture}
\newcommand{\annL}[6] [] {
    \coordinate (AE1) at ($(#2)!#4!90:(#3)$);
    \coordinate (BE1) at ($(#3)!#4!-90:(#2)$);
    \coordinate (AE2) at ($(#2)!#5!90:(#3)$);
    \coordinate (BE2) at ($(#3)!#5!-90:(#2)$);
    \draw[>=latex,red,<->]  (AE2) -- (BE2) node[midway,sloped,below,align=center] {#6};
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#2) -- (AE1);
    \draw[very thin,shorten >=1pt,shorten <=1pt] (#3) -- (BE1);
}
\newcommand{\annC}[6] [] {
   \coordinate (AE1) at ($(#2)!#4!90:(#3)$);
    \coordinate (BE1) at ($(#3)!#4!-90:(#2)$);
   \draw [decorate,decoration={brace,amplitude=#5},xshift=0pt,yshift=0pt]
          (AE1) -- (BE1) node [black,midway,sloped,above,yshift=#5]  {#6} ;
}
\coordinate (A) at (0,0);
\coordinate (B) at (1,2);
\draw (A) -- (B);
\annC{A}{B}{1mm}{0.5mm}{x};
\annL{B}{A}{5mm}{4mm}{y};
\end{tikzpicture}
\end{document}

enter image description here

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.