What I want to do
I want to create a command \planfigur{<edges>}{<angles>}
which draws a triangle using TikZ
. \planfigur
should have 2 mandatory arguments, which let me specify edges and/or angles (where a = alpha, b = beta and c = gamma) of the triangle, which are drawn in red instead of black color.
E. g. \planfigur{ab}{bc}
draws a black triangle, where the edges a and b and the angles beta and gamma are red.
I use expl3
and xparse
. I already have read (Is it reasonable to expect an expl3-compatible release of TikZ/PGF in the future?), that there are problems in using expl3
and tikz
together (which makes sense).
So I played around and tried to outsmart TikZ
. I had the following idea, which unfortunately doesn't work (1:0 for TikZ
:-)). The approach is to move all expl3
code into a new command, where I can use \tl_if_in:nnT
to check the arguments.
Code
\documentclass{article}
\usepackage{expl3, xparse}
\usepackage{tikz}
\usetikzlibrary{calc}
\ExplSyntaxOn
\tl_new:N \__edu_planfigur_temp
\DeclareDocumentCommand \planfigurLines { m } {
\tl_clear:N \__edu_planfigur_temp
\tl_if_in:nnT {#1} {a} {
\tl_put_right:Nn \__edu_planfigur_temp {\draw (b) -- (c);}
}
\tl_if_in:nnT {#1} {b} {
\tl_put_right:Nn \__edu_planfigur_temp {\draw (a) -- (c);}
}
\tl_if_in:nnT {#1} {c} {
\tl_put_right:Nn \__edu_planfigur_temp {\draw (a) -- (b);}
}
\tl_use:N \__edu_planfigur_temp
}
\DeclareExpandableDocumentCommand \planfigurAngles { m } {
\tl_if_in:nnT {#1} {a} {
\tl_put_right:Nn \__edu_planfigur_temp {\draw ($(a) + (0:1)$) arc (0:40:1) ($(a) + (20:0.7)$) node {$\alpha$};}
}
\tl_if_in:nnT {#1} {b} {
\tl_put_right:Nn \__edu_planfigur_temp {\draw ($(b) + (120:1)$) arc (120:180:1) ($(b) + (150:0.7)$) node {$\beta$};}
}
\tl_if_in:nnT {#1} {c} {
\tl_put_right:Nn \__edu_planfigur_temp {\draw ($(c) + (220:1)$) arc (220:300:1) ($(c) + (260:0.65)$) node {$\gamma$};}
}
\tl_use:N \__edu_planfigur_temp
}
\ExplSyntaxOff
\DeclareExpandableDocumentCommand \planfigur { m m } {
\begin{tikzpicture}[scale=0.65, line join=round, thick]
\coordinate (a) at (0,0);
\coordinate (b) at (5,0);
\coordinate (c) at (3.37, 2.83);
\draw (a) -- node[below] {$c$} (b) -- node[above right] {$a$} (c) -- node[above left] {$b$} (a) -- cycle;
\draw (a) node[left] {$A$};
\draw (b) node[right] {$B$};
\draw (c) node[above] {$C$};
\begin{scope}[color=red, fill=red!25, very thick]
\planfigurLines{#1}
\planfigurAngles{#2}
\end{scope}
\end{tikzpicture}
}
\begin{document}
\planfigur{ac}{a}
\end{document}
Error
! Package pgf Error: No shape named 0:1 is known.
See the pgf package documentation for explanation. Type H for immediate help. ...
l.70 \planfigur{ac}{a}
expl3
then:
has the catcode of a letter but TikZ expects it to beother
. One way around this is to define a token list containing a colon with catcode "other" and use that in place of the bare:
in your code.:
. This would also explain, why the lines (which don't contain a:
) work without any problem. Your approach sounds promising, but I don't know how to do this. :-(\tl_const:Nx \c_edu_colon_tl { \token_to_str:N : }
and then use\c_edu_colon_tl
instead of:
in coordinates.\c_edu_colon_tl
then contains a:
with catcode 12 (other) instead of 11 (letter).\planfigur...
commands are not expandable, so they should be declared with\NewDocumentCommand
.