Color bleeding when using vsplit for a box with colored text












7















I want to vsplit a box with quite arbitrary content. If the content does some color changes, strange things happen.



The following MWE demonstrates the problem. The example text changes font and color of the second paragraph. When this paragraph is split, the red color bleeds outside the box, see inter text while the font change is restricted to the box.



By the way: setting the color to black before the inter text worsen the problem, because then the third paragraph and everything after the second box is painted red.



How do I have to adapt the splitmytext macro (and maybe the savemytext macro) to avoid this color bleeding problem?



documentclass{article}
usepackage{xcolor,lipsum}

newboxmwebox
newboxfirstbox

newcommand{savemytext}[2]{%
setboxmweboxvbox{hsize=#1relax#2}}

newcommand{splitmytext}[1]{%
setboxfirstboxvsplitmwebox to #1}

begin{document}

savemytext{10cm}{%
lipsum[1]
{itshapebfseriescolor{red}
lipsum[2]
}
lipsum[3]
}

splitmytext{7cm}

fbox{boxfirstbox}

inter text

fbox{boxmwebox}

after text

end{document}


enter image description here










share|improve this question























  • this is "as expected", but i don't have a clear idea what to do about it. the problem is that colour is always set in a group, and unset immediately after the egroupendgroup. i can't think, off hand, of a way of getting the end-grouping into the end of the top bit, and then add a colour command at the beginning of the bottom bit.

    – wasteofspace
    Dec 20 '13 at 10:07






  • 1





    The hardest part of latex color support was not the color package it was ensuring all the box commands in the latex format didn't break when color was loaded. Welcome to the club:-)

    – David Carlisle
    Dec 20 '13 at 10:30
















7















I want to vsplit a box with quite arbitrary content. If the content does some color changes, strange things happen.



The following MWE demonstrates the problem. The example text changes font and color of the second paragraph. When this paragraph is split, the red color bleeds outside the box, see inter text while the font change is restricted to the box.



By the way: setting the color to black before the inter text worsen the problem, because then the third paragraph and everything after the second box is painted red.



How do I have to adapt the splitmytext macro (and maybe the savemytext macro) to avoid this color bleeding problem?



documentclass{article}
usepackage{xcolor,lipsum}

newboxmwebox
newboxfirstbox

newcommand{savemytext}[2]{%
setboxmweboxvbox{hsize=#1relax#2}}

newcommand{splitmytext}[1]{%
setboxfirstboxvsplitmwebox to #1}

begin{document}

savemytext{10cm}{%
lipsum[1]
{itshapebfseriescolor{red}
lipsum[2]
}
lipsum[3]
}

splitmytext{7cm}

fbox{boxfirstbox}

inter text

fbox{boxmwebox}

after text

end{document}


enter image description here










share|improve this question























  • this is "as expected", but i don't have a clear idea what to do about it. the problem is that colour is always set in a group, and unset immediately after the egroupendgroup. i can't think, off hand, of a way of getting the end-grouping into the end of the top bit, and then add a colour command at the beginning of the bottom bit.

    – wasteofspace
    Dec 20 '13 at 10:07






  • 1





    The hardest part of latex color support was not the color package it was ensuring all the box commands in the latex format didn't break when color was loaded. Welcome to the club:-)

    – David Carlisle
    Dec 20 '13 at 10:30














7












7








7








I want to vsplit a box with quite arbitrary content. If the content does some color changes, strange things happen.



The following MWE demonstrates the problem. The example text changes font and color of the second paragraph. When this paragraph is split, the red color bleeds outside the box, see inter text while the font change is restricted to the box.



By the way: setting the color to black before the inter text worsen the problem, because then the third paragraph and everything after the second box is painted red.



How do I have to adapt the splitmytext macro (and maybe the savemytext macro) to avoid this color bleeding problem?



documentclass{article}
usepackage{xcolor,lipsum}

newboxmwebox
newboxfirstbox

newcommand{savemytext}[2]{%
setboxmweboxvbox{hsize=#1relax#2}}

newcommand{splitmytext}[1]{%
setboxfirstboxvsplitmwebox to #1}

begin{document}

savemytext{10cm}{%
lipsum[1]
{itshapebfseriescolor{red}
lipsum[2]
}
lipsum[3]
}

splitmytext{7cm}

fbox{boxfirstbox}

inter text

fbox{boxmwebox}

after text

end{document}


enter image description here










share|improve this question














I want to vsplit a box with quite arbitrary content. If the content does some color changes, strange things happen.



The following MWE demonstrates the problem. The example text changes font and color of the second paragraph. When this paragraph is split, the red color bleeds outside the box, see inter text while the font change is restricted to the box.



By the way: setting the color to black before the inter text worsen the problem, because then the third paragraph and everything after the second box is painted red.



How do I have to adapt the splitmytext macro (and maybe the savemytext macro) to avoid this color bleeding problem?



documentclass{article}
usepackage{xcolor,lipsum}

newboxmwebox
newboxfirstbox

newcommand{savemytext}[2]{%
setboxmweboxvbox{hsize=#1relax#2}}

newcommand{splitmytext}[1]{%
setboxfirstboxvsplitmwebox to #1}

begin{document}

savemytext{10cm}{%
lipsum[1]
{itshapebfseriescolor{red}
lipsum[2]
}
lipsum[3]
}

splitmytext{7cm}

fbox{boxfirstbox}

inter text

fbox{boxmwebox}

after text

end{document}


enter image description here







color boxes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 20 '13 at 8:36









Thomas F. SturmThomas F. Sturm

19.4k13171




19.4k13171













  • this is "as expected", but i don't have a clear idea what to do about it. the problem is that colour is always set in a group, and unset immediately after the egroupendgroup. i can't think, off hand, of a way of getting the end-grouping into the end of the top bit, and then add a colour command at the beginning of the bottom bit.

    – wasteofspace
    Dec 20 '13 at 10:07






  • 1





    The hardest part of latex color support was not the color package it was ensuring all the box commands in the latex format didn't break when color was loaded. Welcome to the club:-)

    – David Carlisle
    Dec 20 '13 at 10:30



















  • this is "as expected", but i don't have a clear idea what to do about it. the problem is that colour is always set in a group, and unset immediately after the egroupendgroup. i can't think, off hand, of a way of getting the end-grouping into the end of the top bit, and then add a colour command at the beginning of the bottom bit.

    – wasteofspace
    Dec 20 '13 at 10:07






  • 1





    The hardest part of latex color support was not the color package it was ensuring all the box commands in the latex format didn't break when color was loaded. Welcome to the club:-)

    – David Carlisle
    Dec 20 '13 at 10:30

















this is "as expected", but i don't have a clear idea what to do about it. the problem is that colour is always set in a group, and unset immediately after the egroupendgroup. i can't think, off hand, of a way of getting the end-grouping into the end of the top bit, and then add a colour command at the beginning of the bottom bit.

– wasteofspace
Dec 20 '13 at 10:07





this is "as expected", but i don't have a clear idea what to do about it. the problem is that colour is always set in a group, and unset immediately after the egroupendgroup. i can't think, off hand, of a way of getting the end-grouping into the end of the top bit, and then add a colour command at the beginning of the bottom bit.

– wasteofspace
Dec 20 '13 at 10:07




1




1





The hardest part of latex color support was not the color package it was ensuring all the box commands in the latex format didn't break when color was loaded. Welcome to the club:-)

– David Carlisle
Dec 20 '13 at 10:30





The hardest part of latex color support was not the color package it was ensuring all the box commands in the latex format didn't break when color was loaded. Welcome to the club:-)

– David Carlisle
Dec 20 '13 at 10:30










1 Answer
1






active

oldest

votes


















6














Firstly if there is colour around you always need to make sure that your boxes are double grouped so



setboxmweboxvbox{{hsize=#1relax#2endgraf}}


not



  setboxmweboxvbox{hsize=#1relax#2}}


or



  setboxmweboxvbox{color@begingrouphsize=#1relax#2color@endgroup}


which adds the endgraf for you.



The reason for that is color{red} is really



driver-specific-set-color{red}aftergroupreset@color


where reste@color is driver-specific-set-color{currentcolor} or pop-current-color depending on the back end driver.



This is what gives the colour commands the appearance of following TeX grouping, like font commands.



Without the double group, a color command in the top level list of a saved box causes the font setting to be saved in the box, but the colour restore is placed by the aftergroup outside the setting so tries to pop the colour stack at the point of the box save (at which point there may be no colours on the stack). Usually with pdftex this is just ignored (at this point) with dvips on early laserwiters you would crash the printer which is why latex goes to some effort to ensure this doesn't happen if you use supported latex commands (which doesn't include vbox and vsplit:-)



With the double grouping the colour restore comes at the end of the box, inside the box.



However if you then vsplit the box you end up with a color push in one half and a color pop in the other. If you know (by maintaining extra state in the macro layer) that that is the case, you can of course insert primitive pdftex colour commands to do a pop at the end of the first half and re-assert the colour (without doing a colour restore) at the start of the second half.



If however the content is arbitrary user content that may have colour commands you have a problem that in classic tex you can not query the box list to discover the colour specials as as soon as you find a whatsit you can not see past it (there is unskip unbox etc but nounwhatsit. At this point you start to wonder about switching to luatex....






share|improve this answer


























  • I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

    – Thomas F. Sturm
    Dec 20 '13 at 11:33











  • @ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

    – David Carlisle
    Dec 20 '13 at 11:38













  • No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

    – Thomas F. Sturm
    Dec 20 '13 at 11:52











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%2f150780%2fcolor-bleeding-when-using-vsplit-for-a-box-with-colored-text%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









6














Firstly if there is colour around you always need to make sure that your boxes are double grouped so



setboxmweboxvbox{{hsize=#1relax#2endgraf}}


not



  setboxmweboxvbox{hsize=#1relax#2}}


or



  setboxmweboxvbox{color@begingrouphsize=#1relax#2color@endgroup}


which adds the endgraf for you.



The reason for that is color{red} is really



driver-specific-set-color{red}aftergroupreset@color


where reste@color is driver-specific-set-color{currentcolor} or pop-current-color depending on the back end driver.



This is what gives the colour commands the appearance of following TeX grouping, like font commands.



Without the double group, a color command in the top level list of a saved box causes the font setting to be saved in the box, but the colour restore is placed by the aftergroup outside the setting so tries to pop the colour stack at the point of the box save (at which point there may be no colours on the stack). Usually with pdftex this is just ignored (at this point) with dvips on early laserwiters you would crash the printer which is why latex goes to some effort to ensure this doesn't happen if you use supported latex commands (which doesn't include vbox and vsplit:-)



With the double grouping the colour restore comes at the end of the box, inside the box.



However if you then vsplit the box you end up with a color push in one half and a color pop in the other. If you know (by maintaining extra state in the macro layer) that that is the case, you can of course insert primitive pdftex colour commands to do a pop at the end of the first half and re-assert the colour (without doing a colour restore) at the start of the second half.



If however the content is arbitrary user content that may have colour commands you have a problem that in classic tex you can not query the box list to discover the colour specials as as soon as you find a whatsit you can not see past it (there is unskip unbox etc but nounwhatsit. At this point you start to wonder about switching to luatex....






share|improve this answer


























  • I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

    – Thomas F. Sturm
    Dec 20 '13 at 11:33











  • @ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

    – David Carlisle
    Dec 20 '13 at 11:38













  • No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

    – Thomas F. Sturm
    Dec 20 '13 at 11:52
















6














Firstly if there is colour around you always need to make sure that your boxes are double grouped so



setboxmweboxvbox{{hsize=#1relax#2endgraf}}


not



  setboxmweboxvbox{hsize=#1relax#2}}


or



  setboxmweboxvbox{color@begingrouphsize=#1relax#2color@endgroup}


which adds the endgraf for you.



The reason for that is color{red} is really



driver-specific-set-color{red}aftergroupreset@color


where reste@color is driver-specific-set-color{currentcolor} or pop-current-color depending on the back end driver.



This is what gives the colour commands the appearance of following TeX grouping, like font commands.



Without the double group, a color command in the top level list of a saved box causes the font setting to be saved in the box, but the colour restore is placed by the aftergroup outside the setting so tries to pop the colour stack at the point of the box save (at which point there may be no colours on the stack). Usually with pdftex this is just ignored (at this point) with dvips on early laserwiters you would crash the printer which is why latex goes to some effort to ensure this doesn't happen if you use supported latex commands (which doesn't include vbox and vsplit:-)



With the double grouping the colour restore comes at the end of the box, inside the box.



However if you then vsplit the box you end up with a color push in one half and a color pop in the other. If you know (by maintaining extra state in the macro layer) that that is the case, you can of course insert primitive pdftex colour commands to do a pop at the end of the first half and re-assert the colour (without doing a colour restore) at the start of the second half.



If however the content is arbitrary user content that may have colour commands you have a problem that in classic tex you can not query the box list to discover the colour specials as as soon as you find a whatsit you can not see past it (there is unskip unbox etc but nounwhatsit. At this point you start to wonder about switching to luatex....






share|improve this answer


























  • I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

    – Thomas F. Sturm
    Dec 20 '13 at 11:33











  • @ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

    – David Carlisle
    Dec 20 '13 at 11:38













  • No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

    – Thomas F. Sturm
    Dec 20 '13 at 11:52














6












6








6







Firstly if there is colour around you always need to make sure that your boxes are double grouped so



setboxmweboxvbox{{hsize=#1relax#2endgraf}}


not



  setboxmweboxvbox{hsize=#1relax#2}}


or



  setboxmweboxvbox{color@begingrouphsize=#1relax#2color@endgroup}


which adds the endgraf for you.



The reason for that is color{red} is really



driver-specific-set-color{red}aftergroupreset@color


where reste@color is driver-specific-set-color{currentcolor} or pop-current-color depending on the back end driver.



This is what gives the colour commands the appearance of following TeX grouping, like font commands.



Without the double group, a color command in the top level list of a saved box causes the font setting to be saved in the box, but the colour restore is placed by the aftergroup outside the setting so tries to pop the colour stack at the point of the box save (at which point there may be no colours on the stack). Usually with pdftex this is just ignored (at this point) with dvips on early laserwiters you would crash the printer which is why latex goes to some effort to ensure this doesn't happen if you use supported latex commands (which doesn't include vbox and vsplit:-)



With the double grouping the colour restore comes at the end of the box, inside the box.



However if you then vsplit the box you end up with a color push in one half and a color pop in the other. If you know (by maintaining extra state in the macro layer) that that is the case, you can of course insert primitive pdftex colour commands to do a pop at the end of the first half and re-assert the colour (without doing a colour restore) at the start of the second half.



If however the content is arbitrary user content that may have colour commands you have a problem that in classic tex you can not query the box list to discover the colour specials as as soon as you find a whatsit you can not see past it (there is unskip unbox etc but nounwhatsit. At this point you start to wonder about switching to luatex....






share|improve this answer















Firstly if there is colour around you always need to make sure that your boxes are double grouped so



setboxmweboxvbox{{hsize=#1relax#2endgraf}}


not



  setboxmweboxvbox{hsize=#1relax#2}}


or



  setboxmweboxvbox{color@begingrouphsize=#1relax#2color@endgroup}


which adds the endgraf for you.



The reason for that is color{red} is really



driver-specific-set-color{red}aftergroupreset@color


where reste@color is driver-specific-set-color{currentcolor} or pop-current-color depending on the back end driver.



This is what gives the colour commands the appearance of following TeX grouping, like font commands.



Without the double group, a color command in the top level list of a saved box causes the font setting to be saved in the box, but the colour restore is placed by the aftergroup outside the setting so tries to pop the colour stack at the point of the box save (at which point there may be no colours on the stack). Usually with pdftex this is just ignored (at this point) with dvips on early laserwiters you would crash the printer which is why latex goes to some effort to ensure this doesn't happen if you use supported latex commands (which doesn't include vbox and vsplit:-)



With the double grouping the colour restore comes at the end of the box, inside the box.



However if you then vsplit the box you end up with a color push in one half and a color pop in the other. If you know (by maintaining extra state in the macro layer) that that is the case, you can of course insert primitive pdftex colour commands to do a pop at the end of the first half and re-assert the colour (without doing a colour restore) at the start of the second half.



If however the content is arbitrary user content that may have colour commands you have a problem that in classic tex you can not query the box list to discover the colour specials as as soon as you find a whatsit you can not see past it (there is unskip unbox etc but nounwhatsit. At this point you start to wonder about switching to luatex....







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 16 at 20:26









Martin Scharrer

200k45636818




200k45636818










answered Dec 20 '13 at 10:46









David CarlisleDavid Carlisle

487k4111231868




487k4111231868













  • I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

    – Thomas F. Sturm
    Dec 20 '13 at 11:33











  • @ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

    – David Carlisle
    Dec 20 '13 at 11:38













  • No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

    – Thomas F. Sturm
    Dec 20 '13 at 11:52



















  • I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

    – Thomas F. Sturm
    Dec 20 '13 at 11:33











  • @ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

    – David Carlisle
    Dec 20 '13 at 11:38













  • No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

    – Thomas F. Sturm
    Dec 20 '13 at 11:52

















I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

– Thomas F. Sturm
Dec 20 '13 at 11:33





I had my fears that there might be no solving answer to my question. But you never know without asking ;-) I just wonder, if it is possible to clear the color stack completely? Let's say at the end of the last box (assumed it is on the top level) to be sure everything is alright afterwards?

– Thomas F. Sturm
Dec 20 '13 at 11:33













@ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

– David Carlisle
Dec 20 '13 at 11:38







@ThomasF.Sturm not at the tex level, but if you commit to one back end (eg pdftex) it may be possible to inject raw pdf code in at that point to clear its colour stack but you'd need to ask on the pdftex list, if you inject pdf commands into the pdf stream being generated by pdftex you need to have a pretty good idea of the structure of that stream, and it's been a decade or two since I looked at that:-)

– David Carlisle
Dec 20 '13 at 11:38















No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

– Thomas F. Sturm
Dec 20 '13 at 11:52





No, for me it is enough to know that there's no TeX level solution; that keeps me from trying things which cannot work ;-)

– Thomas F. Sturm
Dec 20 '13 at 11:52


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f150780%2fcolor-bleeding-when-using-vsplit-for-a-box-with-colored-text%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents