For straight lines (--
) a special to path
could be the solution.
I have defined three to
styles:
a=<node text>
: relative positioning
a position=<pos amount>
(default 1.1
)
aa=<node text>
: absolute positioning
aa distance=<length>
(default 1ex
)
bb
: absolute positioning but saves the coordinate and the angle for later use:
\xVecN
: the x value,
\yVecN
: the y value,
\aVecB
: the anchor
angle.
- The
bb style
uses these values internally.
aa distance=<length>
(default 1ex
)
The lines in the code sample that are marked with % debug
can be removed; they are only there to show how the styles work.
Code
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{calc}
\newlength{\qrrAadistance}
\setlength{\qrrAadistance}{1ex}
\newcommand*{\qrrAposition}{1.1}
\tikzset{
a style/.style={% this style should be set to further change the behaviour of the nodes that are now hidden inside
draw, % debug
},
a position/.code={\pgfmathsetmacro\qrrAposition{#1}},
a/.style={% relative position
to path={
let \p1=(\tikztostart),
\p2=(\tikztotarget),
\n1={atan2(\x2-\x1,\y2-\y1)} in
-- (\tikztotarget) \tikztonodes node[pos=\qrrAposition, anchor=\n1-180, a style] {#1}
node[pos=\qrrAposition, fill=black, circle, inner sep=0.6pt] {}% debug
}
},
aa distance/.code={\pgfmathsetlength\qrrAadistance{#1}},
aa distance/.initial=1ex,
aa/.style={% fixed distance
to path={
let \p1=(\tikztostart),
\p2=(\tikztotarget),
\n1={atan2(\x2-\x1,\y2-\y1)} in
-- (\tikztotarget) \tikztonodes node[anchor=\n1-180, a style] at ($(\tikztotarget)+(\n1:\the\qrrAadistance)$) {#1}
node[fill=black, circle, inner sep=0.6pt] at ($(\tikztotarget)+(\n1:\the\qrrAadistance)$) {} % debug
}
},
b/.style={% later usage
to path={
let \p1=(\tikztostart),
\p2=(\tikztotarget),
\n1={atan2(\x2-\x1,\y2-\y1)},
\p{node}=($(\tikztotarget)+(\n1:\the\qrrAadistance)$)
in
-- (\tikztotarget) \tikztonodes
\pgfextra{\xdef\aVecN{\n1-180}\xdef\xVecN{\x{node}}\xdef\yVecN{\y{node}}}
}
},
bb style/.style={
anchor=\aVecN,
at={(\xVecN,\yVecN)},
a style,
},
}
\begin{document}
\begin{tikzpicture}
\draw[->] (0, 0) to[a=$\vec{v}$] (1, 3);
\draw[->] (0, 0) to[aa=$\vec{v} + \vec{w}$] (3, 0);
\draw[->] (0, 0) to[bb] (1, -3);
\node[bb style] {$\vec{v} + \vec{w} + (0, -6)$};
\end{tikzpicture}
\end{document}
Output
% debug
How it works
\foreach \angle in {0,2,...,358}{
\begin{tikzpicture}
\path[use as bounding box] (-2.5,-2) -- (2.5,2);
\draw[->] (0, 0) to[aa=$\vec{v} + \vec{w}$] (\angle:1cm);
\end{tikzpicture}
}
Difference between absolute and relative positioning
\foreach \l in {0.1,0.2,...,2.9,3.0,2.9,...,0.2}{
\begin{tikzpicture}
\path (0,0) -- (0:4.5cm);
\draw[->] (0, 0) to[a =$\vec{v} + \vec{w}$] (0:\l);
\draw[yshift=-.7cm,->] (0, 0) to[aa=$\vec{v} + \vec{w}$] (0:\l);
\end{tikzpicture}
}
Output
A solution with small bullets, and your code simplified, using the nccmath
, relsize
and braket
packages. I patched the definition of the \Set
command thanks to David Carlisle to improve the spacing of the braces.
Note that "discr" is actually a math operator, and I declare it as such, in order to have better horizontal spacings.
\documentclass[11pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[textwidth = 15cm]{geometry}
\usepackage{lmodern, amssymb}
\usepackage{mathtools}
\usepackage{braket, relsize, nccmath}
\renewcommand\Set[1]{%
\csname Set \endcsname{\mskip-\medmuskip#1\mskip-\medmuskip}}%
\DeclareMathOperator{\disc}{disc}
\pagestyle{empty}
\begin{document}
\begin{equation}
V_{p}=\Set{%
F\in\Phi | %
\begin{medsize}\begin{array}{@{\mathsmaller{\bullet}\enspace}l@{}}
p^2 \nmid \disc(F) \quad \text{if}\enspace p \neq 2\\[1ex]
\disc(F)\equiv 1\pmod 4 \vee \disc(F)\equiv 8\vee 12
\pmod{16} \quad\text{if} \enspace p =2
\end{array}\end{medsize}
}%
\end{equation}
\end{document}
Best Answer
Another solution is to use a
matrix
. This allows you to put the equation in order easily, and then, if you define the cells of thematrix
to benode
s, you can then use thepositioning
library to position your labels relative to thosenode
s.(Note that the
shorten >=
option is used in this code to make the arrows look a bit better, so they don't immediately brush up against the lines of the individual characters. You can modify as you desire.)