savebox within tikzpicture results in an empty savebox
up vote
8
down vote
favorite
Using savebox
within a tikzpicture
environment seems wipe out the content of the savebox
.
In the MWE below, the following macros define, measure and display the size of a savebox
:
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
As long as DefineBoxAndItSize
is invoked outside of a tikzpicture
, using ShowBoxAndSize
both outside (first line of output) and inside a tikzpicture
environment (second line of output) works fine.
However, the third line invokes DefineBoxAndItSize
inside a tikzpicture
before ShowBoxAndSize
, which ends up making the box empty.
Question:
How can I define a savebox
within a tikzpicture
environment?
Code:
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
tikz-pgf boxes scoping box
|
show 1 more comment
up vote
8
down vote
favorite
Using savebox
within a tikzpicture
environment seems wipe out the content of the savebox
.
In the MWE below, the following macros define, measure and display the size of a savebox
:
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
As long as DefineBoxAndItSize
is invoked outside of a tikzpicture
, using ShowBoxAndSize
both outside (first line of output) and inside a tikzpicture
environment (second line of output) works fine.
However, the third line invokes DefineBoxAndItSize
inside a tikzpicture
before ShowBoxAndSize
, which ends up making the box empty.
Question:
How can I define a savebox
within a tikzpicture
environment?
Code:
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
tikz-pgf boxes scoping box
1
outside nodes the font is nullfont.
– Ulrike Fischer
18 hours ago
the box is saved, but it saves the same text that it would typeset if you used that text at that point. (look in the log for missing character messages)
– David Carlisle
18 hours ago
I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
18 hours ago
@marmot: Yep. Usingbegin{pgfinterruptpicture} DefineBoxAndItSize end{pgfinterruptpicture}
works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question.
– Peter Grill
18 hours ago
@PeterGrill Have you also seen this question? It seems to be slightly closer to yours, and the OP addedbegin{pgfinterruptpicture} ... end{pgfinterruptpicture}
to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better.
– marmot
18 hours ago
|
show 1 more comment
up vote
8
down vote
favorite
up vote
8
down vote
favorite
Using savebox
within a tikzpicture
environment seems wipe out the content of the savebox
.
In the MWE below, the following macros define, measure and display the size of a savebox
:
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
As long as DefineBoxAndItSize
is invoked outside of a tikzpicture
, using ShowBoxAndSize
both outside (first line of output) and inside a tikzpicture
environment (second line of output) works fine.
However, the third line invokes DefineBoxAndItSize
inside a tikzpicture
before ShowBoxAndSize
, which ends up making the box empty.
Question:
How can I define a savebox
within a tikzpicture
environment?
Code:
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
tikz-pgf boxes scoping box
Using savebox
within a tikzpicture
environment seems wipe out the content of the savebox
.
In the MWE below, the following macros define, measure and display the size of a savebox
:
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
As long as DefineBoxAndItSize
is invoked outside of a tikzpicture
, using ShowBoxAndSize
both outside (first line of output) and inside a tikzpicture
environment (second line of output) works fine.
However, the third line invokes DefineBoxAndItSize
inside a tikzpicture
before ShowBoxAndSize
, which ends up making the box empty.
Question:
How can I define a savebox
within a tikzpicture
environment?
Code:
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
newcommand*{DefineBoxAndItSize}{%
saveboxtempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
tikz-pgf boxes scoping box
tikz-pgf boxes scoping box
asked 18 hours ago
Peter Grill
162k24432739
162k24432739
1
outside nodes the font is nullfont.
– Ulrike Fischer
18 hours ago
the box is saved, but it saves the same text that it would typeset if you used that text at that point. (look in the log for missing character messages)
– David Carlisle
18 hours ago
I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
18 hours ago
@marmot: Yep. Usingbegin{pgfinterruptpicture} DefineBoxAndItSize end{pgfinterruptpicture}
works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question.
– Peter Grill
18 hours ago
@PeterGrill Have you also seen this question? It seems to be slightly closer to yours, and the OP addedbegin{pgfinterruptpicture} ... end{pgfinterruptpicture}
to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better.
– marmot
18 hours ago
|
show 1 more comment
1
outside nodes the font is nullfont.
– Ulrike Fischer
18 hours ago
the box is saved, but it saves the same text that it would typeset if you used that text at that point. (look in the log for missing character messages)
– David Carlisle
18 hours ago
I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
18 hours ago
@marmot: Yep. Usingbegin{pgfinterruptpicture} DefineBoxAndItSize end{pgfinterruptpicture}
works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question.
– Peter Grill
18 hours ago
@PeterGrill Have you also seen this question? It seems to be slightly closer to yours, and the OP addedbegin{pgfinterruptpicture} ... end{pgfinterruptpicture}
to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better.
– marmot
18 hours ago
1
1
outside nodes the font is nullfont.
– Ulrike Fischer
18 hours ago
outside nodes the font is nullfont.
– Ulrike Fischer
18 hours ago
the box is saved, but it saves the same text that it would typeset if you used that text at that point. (look in the log for missing character messages)
– David Carlisle
18 hours ago
the box is saved, but it saves the same text that it would typeset if you used that text at that point. (look in the log for missing character messages)
– David Carlisle
18 hours ago
I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
18 hours ago
I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
18 hours ago
@marmot: Yep. Using
begin{pgfinterruptpicture} DefineBoxAndItSize end{pgfinterruptpicture}
works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question.– Peter Grill
18 hours ago
@marmot: Yep. Using
begin{pgfinterruptpicture} DefineBoxAndItSize end{pgfinterruptpicture}
works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question.– Peter Grill
18 hours ago
@PeterGrill Have you also seen this question? It seems to be slightly closer to yours, and the OP added
begin{pgfinterruptpicture} ... end{pgfinterruptpicture}
to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better.– marmot
18 hours ago
@PeterGrill Have you also seen this question? It seems to be slightly closer to yours, and the OP added
begin{pgfinterruptpicture} ... end{pgfinterruptpicture}
to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better.– marmot
18 hours ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
Background: TikZ happens to gobble text unless it is an argument of a node. Even though this is sort of well known, this also has less obvious implications. In particular, saveboxes may end up being empty. This has already lead to confusion here and here. In the latter post, the OP solves the issue by wrapping their macro in begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
.
The next step of sophistication, which may or may not deserve a new answer, might then be to wrap the savebox definition in an begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
only if the macro is called inside a tikzpicture
environment. To this end, the tests of this post and this post are employed. (Let me remark that the IfInTikzPic
is a bit unusual in that its first argument is the action that is to be taken if one is not in a tikzpicture
environment. Whether or not this is related to the author driving on the other side of the road, I don't know. ;-)
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
makeatletter% from https://tex.stackexchange.com/a/458855/121799
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DefineBoxAndItSize}{%
IfInTikzPic{saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}{begin{pgfinterruptpicture}%
saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
end{pgfinterruptpicture}}
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
I'll be happy to remove this answer if people feel that this question is a duplicate and thus does not deserve a separate answer. (However, at the time of writing this, I am reputation capped, so the reason is certainly not to harvest reputation points, which it is in none of my posts.)
EDIT: Fixed a space, big thanks to Phelype Oleinik!
1
Deserves one upvote solely for the remark onIfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a%
afterbegin{pgfinterruptpicture}
to align the last sentence.
– Phelype Oleinik
17 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
1
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
Background: TikZ happens to gobble text unless it is an argument of a node. Even though this is sort of well known, this also has less obvious implications. In particular, saveboxes may end up being empty. This has already lead to confusion here and here. In the latter post, the OP solves the issue by wrapping their macro in begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
.
The next step of sophistication, which may or may not deserve a new answer, might then be to wrap the savebox definition in an begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
only if the macro is called inside a tikzpicture
environment. To this end, the tests of this post and this post are employed. (Let me remark that the IfInTikzPic
is a bit unusual in that its first argument is the action that is to be taken if one is not in a tikzpicture
environment. Whether or not this is related to the author driving on the other side of the road, I don't know. ;-)
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
makeatletter% from https://tex.stackexchange.com/a/458855/121799
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DefineBoxAndItSize}{%
IfInTikzPic{saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}{begin{pgfinterruptpicture}%
saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
end{pgfinterruptpicture}}
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
I'll be happy to remove this answer if people feel that this question is a duplicate and thus does not deserve a separate answer. (However, at the time of writing this, I am reputation capped, so the reason is certainly not to harvest reputation points, which it is in none of my posts.)
EDIT: Fixed a space, big thanks to Phelype Oleinik!
1
Deserves one upvote solely for the remark onIfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a%
afterbegin{pgfinterruptpicture}
to align the last sentence.
– Phelype Oleinik
17 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
1
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
add a comment |
up vote
9
down vote
accepted
Background: TikZ happens to gobble text unless it is an argument of a node. Even though this is sort of well known, this also has less obvious implications. In particular, saveboxes may end up being empty. This has already lead to confusion here and here. In the latter post, the OP solves the issue by wrapping their macro in begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
.
The next step of sophistication, which may or may not deserve a new answer, might then be to wrap the savebox definition in an begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
only if the macro is called inside a tikzpicture
environment. To this end, the tests of this post and this post are employed. (Let me remark that the IfInTikzPic
is a bit unusual in that its first argument is the action that is to be taken if one is not in a tikzpicture
environment. Whether or not this is related to the author driving on the other side of the road, I don't know. ;-)
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
makeatletter% from https://tex.stackexchange.com/a/458855/121799
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DefineBoxAndItSize}{%
IfInTikzPic{saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}{begin{pgfinterruptpicture}%
saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
end{pgfinterruptpicture}}
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
I'll be happy to remove this answer if people feel that this question is a duplicate and thus does not deserve a separate answer. (However, at the time of writing this, I am reputation capped, so the reason is certainly not to harvest reputation points, which it is in none of my posts.)
EDIT: Fixed a space, big thanks to Phelype Oleinik!
1
Deserves one upvote solely for the remark onIfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a%
afterbegin{pgfinterruptpicture}
to align the last sentence.
– Phelype Oleinik
17 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
1
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
add a comment |
up vote
9
down vote
accepted
up vote
9
down vote
accepted
Background: TikZ happens to gobble text unless it is an argument of a node. Even though this is sort of well known, this also has less obvious implications. In particular, saveboxes may end up being empty. This has already lead to confusion here and here. In the latter post, the OP solves the issue by wrapping their macro in begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
.
The next step of sophistication, which may or may not deserve a new answer, might then be to wrap the savebox definition in an begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
only if the macro is called inside a tikzpicture
environment. To this end, the tests of this post and this post are employed. (Let me remark that the IfInTikzPic
is a bit unusual in that its first argument is the action that is to be taken if one is not in a tikzpicture
environment. Whether or not this is related to the author driving on the other side of the road, I don't know. ;-)
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
makeatletter% from https://tex.stackexchange.com/a/458855/121799
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DefineBoxAndItSize}{%
IfInTikzPic{saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}{begin{pgfinterruptpicture}%
saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
end{pgfinterruptpicture}}
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
I'll be happy to remove this answer if people feel that this question is a duplicate and thus does not deserve a separate answer. (However, at the time of writing this, I am reputation capped, so the reason is certainly not to harvest reputation points, which it is in none of my posts.)
EDIT: Fixed a space, big thanks to Phelype Oleinik!
Background: TikZ happens to gobble text unless it is an argument of a node. Even though this is sort of well known, this also has less obvious implications. In particular, saveboxes may end up being empty. This has already lead to confusion here and here. In the latter post, the OP solves the issue by wrapping their macro in begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
.
The next step of sophistication, which may or may not deserve a new answer, might then be to wrap the savebox definition in an begin{pgfinterruptpicture}
and end{pgfinterruptpicture}
only if the macro is called inside a tikzpicture
environment. To this end, the tests of this post and this post are employed. (Let me remark that the IfInTikzPic
is a bit unusual in that its first argument is the action that is to be taken if one is not in a tikzpicture
environment. Whether or not this is related to the author driving on the other side of the road, I don't know. ;-)
documentclass[10pt]{article}
usepackage{tikz}
usepackage{printlen}
newsavebox{tempboxA}
newlength{SizeOfBox}
fboxsep=0pt
makeatletter% from https://tex.stackexchange.com/a/458855/121799
newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
}
makeatother
newcommand*{DefineBoxAndItSize}{%
IfInTikzPic{saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
}{begin{pgfinterruptpicture}%
saveboxtempboxA{Hg}%
% https://tex.stackexchange.com/a/11945/4301
setlengthSizeOfBox{dimexprhttempboxA+dptempboxArelax}%
end{pgfinterruptpicture}}
}%
newcommand*{ShowBoxAndSize}{%
Box fbox{usebox{tempboxA}} is printlength{SizeOfBox}%
}
begin{document}
DefineBoxAndItSize%
noindent
Outside TikZ: ShowBoxAndSize.
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
node at (0,0) {Use inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
medskipnoindent
begin{tikzpicture}[inner sep=0pt, anchor=east]
DefineBoxAndItSize% <--- What is wrong with this?
node at (0,-1) {Define inside TikZ: ShowBoxAndSize.};
end{tikzpicture}%
end{document}
I'll be happy to remove this answer if people feel that this question is a duplicate and thus does not deserve a separate answer. (However, at the time of writing this, I am reputation capped, so the reason is certainly not to harvest reputation points, which it is in none of my posts.)
EDIT: Fixed a space, big thanks to Phelype Oleinik!
edited 16 hours ago
answered 17 hours ago
marmot
74.8k482158
74.8k482158
1
Deserves one upvote solely for the remark onIfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a%
afterbegin{pgfinterruptpicture}
to align the last sentence.
– Phelype Oleinik
17 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
1
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
add a comment |
1
Deserves one upvote solely for the remark onIfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a%
afterbegin{pgfinterruptpicture}
to align the last sentence.
– Phelype Oleinik
17 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
1
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
1
1
Deserves one upvote solely for the remark on
IfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a %
after begin{pgfinterruptpicture}
to align the last sentence.– Phelype Oleinik
17 hours ago
Deserves one upvote solely for the remark on
IfInTikzPic
. Brilliant! (and for the answer, of course). One comment: I'd add a %
after begin{pgfinterruptpicture}
to align the last sentence.– Phelype Oleinik
17 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
Yes, it is rather well know, until you encounter an error and spend time trying to figure out what is going on, without remembering that detail! :-(
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
@marmot: Nah, its fine. I just end up asking too many embarrassing questions!!! As always, I continue to blame it on lack of sleep.
– Peter Grill
16 hours ago
1
1
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
@PeterGrill Yes, nothing is better than a good amount of hibernation time. (And no, I don't think it is an embarrassing question. I just happen to have come across this a few months earlier.) Ah, I also edited the answer.
– marmot
16 hours ago
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%2f459853%2fsavebox-within-tikzpicture-results-in-an-empty-savebox%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
outside nodes the font is nullfont.
– Ulrike Fischer
18 hours ago
the box is saved, but it saves the same text that it would typeset if you used that text at that point. (look in the log for missing character messages)
– David Carlisle
18 hours ago
I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
18 hours ago
@marmot: Yep. Using
begin{pgfinterruptpicture} DefineBoxAndItSize end{pgfinterruptpicture}
works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question.– Peter Grill
18 hours ago
@PeterGrill Have you also seen this question? It seems to be slightly closer to yours, and the OP added
begin{pgfinterruptpicture} ... end{pgfinterruptpicture}
to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better.– marmot
18 hours ago