savebox within tikzpicture results in an empty savebox











up vote
9
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.



enter image description here



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}









share|improve this question


















  • 1




    outside nodes the font is nullfont.
    – Ulrike Fischer
    yesterday










  • 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
    yesterday










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    yesterday










  • @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
    yesterday










  • @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
    yesterday















up vote
9
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.



enter image description here



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}









share|improve this question


















  • 1




    outside nodes the font is nullfont.
    – Ulrike Fischer
    yesterday










  • 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
    yesterday










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    yesterday










  • @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
    yesterday










  • @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
    yesterday













up vote
9
down vote

favorite









up vote
9
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.



enter image description here



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}









share|improve this question













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.



enter image description here



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Peter Grill

162k24432739




162k24432739








  • 1




    outside nodes the font is nullfont.
    – Ulrike Fischer
    yesterday










  • 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
    yesterday










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    yesterday










  • @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
    yesterday










  • @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
    yesterday














  • 1




    outside nodes the font is nullfont.
    – Ulrike Fischer
    yesterday










  • 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
    yesterday










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    yesterday










  • @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
    yesterday










  • @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
    yesterday








1




1




outside nodes the font is nullfont.
– Ulrike Fischer
yesterday




outside nodes the font is nullfont.
– Ulrike Fischer
yesterday












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
yesterday




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
yesterday












I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
yesterday




I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
yesterday












@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
yesterday




@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
yesterday












@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
yesterday




@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
yesterday










1 Answer
1






active

oldest

votes

















up vote
10
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}


enter image description here



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!






share|improve this answer



















  • 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
    yesterday










  • 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
    yesterday










  • @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
    yesterday






  • 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
    yesterday













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
10
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}


enter image description here



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!






share|improve this answer



















  • 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
    yesterday










  • 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
    yesterday










  • @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
    yesterday






  • 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
    yesterday

















up vote
10
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}


enter image description here



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!






share|improve this answer



















  • 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
    yesterday










  • 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
    yesterday










  • @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
    yesterday






  • 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
    yesterday















up vote
10
down vote



accepted







up vote
10
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}


enter image description here



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!






share|improve this answer














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}


enter image description here



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!







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









marmot

74.9k483159




74.9k483159








  • 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
    yesterday










  • 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
    yesterday










  • @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
    yesterday






  • 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
    yesterday
















  • 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
    yesterday










  • 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
    yesterday










  • @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
    yesterday






  • 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
    yesterday










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
yesterday




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
yesterday












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
yesterday




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
yesterday












@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
yesterday




@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
yesterday




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
yesterday






@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
yesterday




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Morgemoulin

Scott Moir

Souastre