savebox within tikzpicture results in an empty savebox












12














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


















  • 2




    outside nodes the font is nullfont.
    – Ulrike Fischer
    Nov 13 '18 at 21:43










  • 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
    Nov 13 '18 at 21:52










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    Nov 13 '18 at 21:54










  • @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
    Nov 13 '18 at 22:09










  • @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
    Nov 13 '18 at 22:16
















12














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


















  • 2




    outside nodes the font is nullfont.
    – Ulrike Fischer
    Nov 13 '18 at 21:43










  • 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
    Nov 13 '18 at 21:52










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    Nov 13 '18 at 21:54










  • @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
    Nov 13 '18 at 22:09










  • @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
    Nov 13 '18 at 22:16














12












12








12







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 Nov 13 '18 at 21:38









Peter Grill

164k25435746




164k25435746








  • 2




    outside nodes the font is nullfont.
    – Ulrike Fischer
    Nov 13 '18 at 21:43










  • 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
    Nov 13 '18 at 21:52










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    Nov 13 '18 at 21:54










  • @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
    Nov 13 '18 at 22:09










  • @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
    Nov 13 '18 at 22:16














  • 2




    outside nodes the font is nullfont.
    – Ulrike Fischer
    Nov 13 '18 at 21:43










  • 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
    Nov 13 '18 at 21:52










  • I guess this is the same as tex.stackexchange.com/q/448576/121799
    – marmot
    Nov 13 '18 at 21:54










  • @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
    Nov 13 '18 at 22:09










  • @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
    Nov 13 '18 at 22:16








2




2




outside nodes the font is nullfont.
– Ulrike Fischer
Nov 13 '18 at 21:43




outside nodes the font is nullfont.
– Ulrike Fischer
Nov 13 '18 at 21:43












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
Nov 13 '18 at 21:52




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
Nov 13 '18 at 21:52












I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
Nov 13 '18 at 21:54




I guess this is the same as tex.stackexchange.com/q/448576/121799
– marmot
Nov 13 '18 at 21:54












@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
Nov 13 '18 at 22:09




@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
Nov 13 '18 at 22:09












@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
Nov 13 '18 at 22:16




@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
Nov 13 '18 at 22:16










1 Answer
1






active

oldest

votes


















12














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



















  • 2




    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
    Nov 13 '18 at 22:57






  • 1




    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
    Nov 13 '18 at 23:47










  • @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
    Nov 13 '18 at 23:51






  • 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
    Nov 13 '18 at 23:53













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',
autoActivateHeartbeat: false,
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















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









12














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



















  • 2




    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
    Nov 13 '18 at 22:57






  • 1




    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
    Nov 13 '18 at 23:47










  • @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
    Nov 13 '18 at 23:51






  • 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
    Nov 13 '18 at 23:53


















12














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



















  • 2




    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
    Nov 13 '18 at 22:57






  • 1




    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
    Nov 13 '18 at 23:47










  • @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
    Nov 13 '18 at 23:51






  • 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
    Nov 13 '18 at 23:53
















12












12








12






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 Nov 13 '18 at 23:51

























answered Nov 13 '18 at 22:49









marmot

87.7k4101189




87.7k4101189








  • 2




    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
    Nov 13 '18 at 22:57






  • 1




    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
    Nov 13 '18 at 23:47










  • @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
    Nov 13 '18 at 23:51






  • 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
    Nov 13 '18 at 23:53
















  • 2




    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
    Nov 13 '18 at 22:57






  • 1




    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
    Nov 13 '18 at 23:47










  • @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
    Nov 13 '18 at 23:51






  • 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
    Nov 13 '18 at 23:53










2




2




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
Nov 13 '18 at 22:57




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
Nov 13 '18 at 22:57




1




1




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
Nov 13 '18 at 23:47




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
Nov 13 '18 at 23:47












@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
Nov 13 '18 at 23:51




@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
Nov 13 '18 at 23:51




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
Nov 13 '18 at 23:53






@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
Nov 13 '18 at 23:53




















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Run scheduled task as local user group (not BUILTIN)

Port of Spain