3

I always find my self doing a lot of repetition with fixed TikZ code blocks that I have to rewrite over and over again. My question is that, is it possible to create a block of TikZ executable commands (not just styles) inside a single argument and call that argument as a style?

Consider this:

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{tikzpicture}
\coordinate [label = below:A](A) at (0, 0);
\coordinate [label = above:B](B) at (3, 3);
\draw (A) -- (B);
\draw [|-|, thin] ($(A)! 8mm !90:(B)$)  -- node [sloped, midway, fill = white]{$3$cm}
($(B)! -8mm !90:(A)$);%This is too long and It would awesome to abstract it
\end{tikzpicture}

enter image description here

What I would like to know is if there any thing like

\tikzset{
dimension line/.code arg={from#1to#2value#3}
    draw the dimension line between (A) and (B),
    add a node in the middle with blah style and blau value
}

So that using

\draw[dimension line=from A to B value 3cm] (A)--(B);

will yield the same output. That will help do the job with minimal effort and allow abstraction, I find this example simple enough in order to under stand how to create code blocks in TikZ. Thank you.

iDrwish
  • 270
  • Some ideas: http://tex.stackexchange.com/questions/14901/dimensioning-of-a-technical-drawing-in-tikz, http://tex.stackexchange.com/questions/123913/adding-dimensions-to-tikz-pictures/123918#123918 – Ignasi Feb 25 '16 at 11:07
  • And another one: Package tikz-dimline – Ignasi Feb 25 '16 at 11:11
  • The other two questions are great and insightful, and for the package, it has a major setback, it doesn't accept arguments but Cartesian coordinates in the form of (x, y), do you know a way around that? – iDrwish Feb 25 '16 at 11:33

1 Answers1

3

This is more a long comment than a real answer.

Although all tikz-dimline examples use cartesian coordinates, it's possible to use named ones:

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz-dimline}

\begin{document}
\begin{tikzpicture}

\draw[red] (0,0) coordinate (A) -- (45:3) coordinate (B);
\dimline[color=blue]{([shift={(-3mm,3mm)}]A)}{([shift={(-3mm,3mm)}]B)}{3cm};
\end{tikzpicture}
\end{document}

enter image description here

In any case, I've finally found the question I was looking for:

Draw dimension of a line as a decoration in TikZ

Ignasi
  • 136,588