# How can I use rotate inside a defined TikZ command?

Here is a simple file defining a TikZ command with an input for drawing options. That input works for options such as dashed or dotted. But it ignores the rotate and scale options. It runs fine with them, but it runs just as if they were not there.

The actual diagrams I want to draw are much more complicated and would benefit if I could use rotate and scale options. Can I do that somehow?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
$\begin{tikzpicture} \def\line[#1](#2)% Syntax: [draw options] (lefthand endpoint {\node at (#2) (A) {\bullet}; \node at ((#2)+(1,0)) (B) {\bullet}; \draw[#1] (A) edge[] (B);} \line[rotate=30,dashed](0,0); \end{tikzpicture}$
\end{document}

• Are you aware of the scope environment? – marmot Jan 8 '18 at 2:38

This is a very quick fix.

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
$\begin{tikzpicture} \def\line[#1](#2)% Syntax: [draw options] (lefthand endpoint {\begin{scope}[#1] \node at (#2) (A) {\bullet}; \node at ((#2)+(1,0)) (B) {\bullet}; \draw (A) edge[] (B); \end{scope}} \line[rotate=30,dashed](0,0); \end{tikzpicture}$
\end{document}


However, I'd recommend to use a \newcommand instead.

• I will probably end up using both, in different places. – Colin McLarty Jan 8 '18 at 2:53

The main reason why rotate does not do anything here is that it should not do anything: you are asking for a line to be drawn between two explicit points (A) and (B), so there is nothing to rotate! Typical uses for rotate inside a tikz \draw command are when you have a decoration or border shape to rotate. If you want to draw a line of length one that can be rotated then you should not use explicit named coordinates. The code below produces,

which is what I think you want. Here is the code:

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xparse}
\NewDocumentCommand\Line{ O{} r()}{% Syntax: [draw options] (lefthand endpoint
\draw[#1] (#2) node{$\bullet$} -- ++(1,0);
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\Line[rotate=30,dashed](0,0);
\Line(2,0);
\Line[rotate=90, blue](2,0);
\end{tikzpicture}
\end{center}
\end{document}


I have used \NewDocumentCommand from the xparse package to define the \Line command (I didn't use \line because there is already a \line command in LaTeX), so that it takes one optional argument and a mandatory argument enclosed in brackets. The line is drawn using relative coordinates ++(1,0) for the second point, so there is no need for the tikz library calc or to define the coordinates (A) and (B). Finally, I have defined \Line outside of the tikzpicture environment so that you can use it in different tikzpicture environments.

• I do not understand why you say that rotate here should not do anything. Adding the scope environment (as in Marmot's answer) makes it do what I wanted. – Colin McLarty Jan 9 '18 at 18:36
• @ColinMcLarty That’s because in marmot’s code the rotate=30 is given as an argument to the scopes environment, which is called as \begin{scope}[rotate=30,dashed]....\end{scope}. In his code the entire environment is rotated and all \draw commands inside the scope will have dashed applied to them. – Andrew Jan 10 '18 at 6:43
• I appreciate your effort and I believe both you and marmot when you say somehow the quick and dirty fix is not best. But it does exactly what I asked for and that is what i want and i do not understand what you mean is the problem. As you say, the arguments rotate and dashed get applied to every draw inside the scope. I did mention the code I sent is just part of a much larger graphic. That is why I use calculate. Marmot's answer lets me correctly draw that larger graphic. – Colin McLarty Jan 10 '18 at 11:24
• @ColinMcLarty If in your real application you want to rotate all of the code injected by the \line command then the best approach is to put everything inside a scope environment because this will rotate your coordinate system. I think that this is the correct approach rather than a dirty fix. What I was saying is that your MWE shouldn't work because you are asking \draw to draw a line between predefined points (A) and (B) and rotate will not move these points. Using scope, however, you rotate before defining (A) and (B) so the line is also rotated, which is what you want. – Andrew Jan 10 '18 at 12:41