Detecting if inside a tikzpicture
up vote
8
down vote
favorite
I would like to define the DrawLine
macro below so that it can be invoked from within a {tikzpicture}
environment or from outside:
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
This works fine when invoked from within a {tikzpicture}
, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:
Missing endgroup inserted.
The MWE below uses the first reference below. The other two references also fail on the commented out test case.
References:
Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a
tikzpicture
(as the commented out code in the MWE shows).Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.
How can I check if the current code is inside a certain environment?.
Code:
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
%DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
tikz-pgf grouping
add a comment |
up vote
8
down vote
favorite
I would like to define the DrawLine
macro below so that it can be invoked from within a {tikzpicture}
environment or from outside:
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
This works fine when invoked from within a {tikzpicture}
, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:
Missing endgroup inserted.
The MWE below uses the first reference below. The other two references also fail on the commented out test case.
References:
Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a
tikzpicture
(as the commented out code in the MWE shows).Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.
How can I check if the current code is inside a certain environment?.
Code:
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
%DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
tikz-pgf grouping
1
tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday
Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday
@TorbjørnT.: That works for all but the commented out case below (yields a different error message though:Undefined control sequence
).
– Peter Grill
yesterday
@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
yesterday
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I would like to define the DrawLine
macro below so that it can be invoked from within a {tikzpicture}
environment or from outside:
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
This works fine when invoked from within a {tikzpicture}
, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:
Missing endgroup inserted.
The MWE below uses the first reference below. The other two references also fail on the commented out test case.
References:
Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a
tikzpicture
(as the commented out code in the MWE shows).Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.
How can I check if the current code is inside a certain environment?.
Code:
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
%DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
tikz-pgf grouping
I would like to define the DrawLine
macro below so that it can be invoked from within a {tikzpicture}
environment or from outside:
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
This works fine when invoked from within a {tikzpicture}
, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:
Missing endgroup inserted.
The MWE below uses the first reference below. The other two references also fail on the commented out test case.
References:
Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a
tikzpicture
(as the commented out code in the MWE shows).Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.
How can I check if the current code is inside a certain environment?.
Code:
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
%DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
tikz-pgf grouping
tikz-pgf grouping
edited yesterday
asked yesterday
Peter Grill
162k24432738
162k24432738
1
tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday
Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday
@TorbjørnT.: That works for all but the commented out case below (yields a different error message though:Undefined control sequence
).
– Peter Grill
yesterday
@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
yesterday
add a comment |
1
tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday
Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday
@TorbjørnT.: That works for all but the commented out case below (yields a different error message though:Undefined control sequence
).
– Peter Grill
yesterday
@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
yesterday
1
1
tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday
tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday
Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday
Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday
@TorbjørnT.: That works for all but the commented out case below (yields a different error message though:
Undefined control sequence
).– Peter Grill
yesterday
@TorbjørnT.: That works for all but the commented out case below (yields a different error message though:
Undefined control sequence
).– Peter Grill
yesterday
@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
yesterday
@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
6
down vote
The following example defines IfInTikzPic
to check the curr
ent envir
onment against tikzpicture
and define a tikzstart
...tikzend
pair accordingly:
documentclass{article}
usepackage{tikz}
makeatletter
def@tikzenvironment{tikzpicture}
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandtikzpicture[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
}%
}
newcommand*{DrawLine}[1]{%
IfInTikzPic
tikzstart
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
tikzend%
}
makeatother
begin{document}
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
You could also group the tikzpicture
if needed:
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandbegin{tikzpicture}[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandend{tikzpicture}%
fi
}%
}
add a comment |
up vote
5
down vote
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic
{begin{tikzpicture}[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
end{tikzpicture}}%
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
textbf{IfInTikzPic}par
IfInTikzPic{inside}{outside}
begin{tikzpicture}
IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
Or this version which directly forces tikz rather than having an if-then-else construct
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandaftertikzify
else
expandafter@firstofone
fi
}
makeatother
deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}
newcommand*{DrawLine}[1]{%
ensuretikz
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
2
@PeterGrill I think the main error wasIfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
– David Carlisle
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
The following example defines IfInTikzPic
to check the curr
ent envir
onment against tikzpicture
and define a tikzstart
...tikzend
pair accordingly:
documentclass{article}
usepackage{tikz}
makeatletter
def@tikzenvironment{tikzpicture}
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandtikzpicture[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
}%
}
newcommand*{DrawLine}[1]{%
IfInTikzPic
tikzstart
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
tikzend%
}
makeatother
begin{document}
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
You could also group the tikzpicture
if needed:
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandbegin{tikzpicture}[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandend{tikzpicture}%
fi
}%
}
add a comment |
up vote
6
down vote
The following example defines IfInTikzPic
to check the curr
ent envir
onment against tikzpicture
and define a tikzstart
...tikzend
pair accordingly:
documentclass{article}
usepackage{tikz}
makeatletter
def@tikzenvironment{tikzpicture}
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandtikzpicture[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
}%
}
newcommand*{DrawLine}[1]{%
IfInTikzPic
tikzstart
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
tikzend%
}
makeatother
begin{document}
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
You could also group the tikzpicture
if needed:
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandbegin{tikzpicture}[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandend{tikzpicture}%
fi
}%
}
add a comment |
up vote
6
down vote
up vote
6
down vote
The following example defines IfInTikzPic
to check the curr
ent envir
onment against tikzpicture
and define a tikzstart
...tikzend
pair accordingly:
documentclass{article}
usepackage{tikz}
makeatletter
def@tikzenvironment{tikzpicture}
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandtikzpicture[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
}%
}
newcommand*{DrawLine}[1]{%
IfInTikzPic
tikzstart
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
tikzend%
}
makeatother
begin{document}
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
You could also group the tikzpicture
if needed:
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandbegin{tikzpicture}[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandend{tikzpicture}%
fi
}%
}
The following example defines IfInTikzPic
to check the curr
ent envir
onment against tikzpicture
and define a tikzstart
...tikzend
pair accordingly:
documentclass{article}
usepackage{tikz}
makeatletter
def@tikzenvironment{tikzpicture}
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandtikzpicture[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
}%
}
newcommand*{DrawLine}[1]{%
IfInTikzPic
tikzstart
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
tikzend%
}
makeatother
begin{document}
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
You could also group the tikzpicture
if needed:
newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandbegin{tikzpicture}[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandend{tikzpicture}%
fi
}%
}
edited yesterday
answered yesterday
Werner
429k589401618
429k589401618
add a comment |
add a comment |
up vote
5
down vote
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic
{begin{tikzpicture}[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
end{tikzpicture}}%
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
textbf{IfInTikzPic}par
IfInTikzPic{inside}{outside}
begin{tikzpicture}
IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
Or this version which directly forces tikz rather than having an if-then-else construct
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandaftertikzify
else
expandafter@firstofone
fi
}
makeatother
deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}
newcommand*{DrawLine}[1]{%
ensuretikz
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
2
@PeterGrill I think the main error wasIfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
– David Carlisle
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
add a comment |
up vote
5
down vote
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic
{begin{tikzpicture}[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
end{tikzpicture}}%
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
textbf{IfInTikzPic}par
IfInTikzPic{inside}{outside}
begin{tikzpicture}
IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
Or this version which directly forces tikz rather than having an if-then-else construct
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandaftertikzify
else
expandafter@firstofone
fi
}
makeatother
deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}
newcommand*{DrawLine}[1]{%
ensuretikz
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
2
@PeterGrill I think the main error wasIfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
– David Carlisle
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
add a comment |
up vote
5
down vote
up vote
5
down vote
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic
{begin{tikzpicture}[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
end{tikzpicture}}%
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
textbf{IfInTikzPic}par
IfInTikzPic{inside}{outside}
begin{tikzpicture}
IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
Or this version which directly forces tikz rather than having an if-then-else construct
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandaftertikzify
else
expandafter@firstofone
fi
}
makeatother
deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}
newcommand*{DrawLine}[1]{%
ensuretikz
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DrawLine}[1]{%
IfInTikzPic
{begin{tikzpicture}[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
end{tikzpicture}}%
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
textbf{IfInTikzPic}par
IfInTikzPic{inside}{outside}
begin{tikzpicture}
IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
Or this version which directly forces tikz rather than having an if-then-else construct
documentclass{article}
usepackage{tikz}
makeatletter
newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandaftertikzify
else
expandafter@firstofone
fi
}
makeatother
deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}
newcommand*{DrawLine}[1]{%
ensuretikz
{draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
}
begin{document}
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%
medskip% --------------------------
textbf{DrawLine}: Actual Outputpar
DrawLine{blue}% <---- How do I get this case to work?
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
medskip% --------------------------
textbf{DrawLine}: Desired Outputpar
begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}
begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}
edited yesterday
answered yesterday
David Carlisle
475k3811051840
475k3811051840
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
2
@PeterGrill I think the main error wasIfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
– David Carlisle
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
add a comment |
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
2
@PeterGrill I think the main error wasIfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
– David Carlisle
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
I thought it might have something to dot with the dreaded expansion issues!
– Peter Grill
yesterday
2
2
@PeterGrill I think the main error was
IfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.– David Carlisle
yesterday
@PeterGrill I think the main error was
IfInTikzPic{}{endtikzpicture}%
as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.– David Carlisle
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
– Peter Grill
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
@PeterGrill plan b posted
– David Carlisle
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f458849%2fdetecting-if-inside-a-tikzpicture%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday
Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday
@TorbjørnT.: That works for all but the commented out case below (yields a different error message though:
Undefined control sequence
).– Peter Grill
yesterday
@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
yesterday