Why NewDocumentEnvironment within ExplSyntaxOn/Off removes spaces from content passed to it outside of ExplSyntaxOn/Off?What do makeatletter and makeatother do?Text within ExplSyntaxOn/OffWhy spaces are ignored even in textual arguments within ExplSyntaxOn… ExplSyntaxOff?

Why do some professors with PhDs leave their professorships to teach high school?

Is "Busen" just the area between the breasts?

How is hair tissue mineral analysis performed?

How did Bellatrix know about the Philosopher's Stone?

Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?

What is "industrial ethernet"?

Array initialization optimization

Do I have any obligations to my PhD supervisor's requests after I have graduated?

Has there been any indication at all that further negotiation between the UK and EU is possible?

How much will studying magic in an academy cost?

Hot coffee brewing solutions for deep woods camping

What is the legal status of travelling with methadone in your carry-on?

Drawing people along with x and y axis

What does "play with your toy’s toys" mean?

Why tighten down in a criss-cross pattern?

Why don't countries like Japan just print more money?

Methodology: Writing unit tests for another developer

Do I have to explain the mechanical superiority of the player-character within the fiction of the game?

Explain why a line can never intersect a plane in exactly two points.

Improving triangulation on AutoCAD-generated stl files

Does having had a visa for a country mean I used to be a citizen/national of that country?

What was the Shuttle Carrier Aircraft escape tunnel?

Would it be a copyright violation if I made a character’s full name refer to a song?

Employer wants to use my work email account after I quit



Why NewDocumentEnvironment within ExplSyntaxOn/Off removes spaces from content passed to it outside of ExplSyntaxOn/Off?


What do makeatletter and makeatother do?Text within ExplSyntaxOn/OffWhy spaces are ignored even in textual arguments within ExplSyntaxOn… ExplSyntaxOff?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I thought turning syntax on/off at appropriate places would make everything work, but I can't use ExplSyntaxOn/Off inside NewDocumentEnvironment so the following code sample doesn't work:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
ExplSyntaxOn
dim_new:NmyDim % DOESN'T WORK
ExplSyntaxOff


% END PARBOX

beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


So I have to enclose the environment within syntax switches, as follows, but then anything passed to the environment loses its spaces:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


% END PARBOX

ExplSyntaxOff % BROUGHT OUTSIDE
% SPACES OF THE FOLLOWING CONTENT ARE STRIPPED
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


How do I retain spaces of the passed content without replacing them with ~ symbols?



By the way, following doesn't strip spaces (but lacks parbox as I need it):



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

dim_new:NmyDim % WORKS (NO SPACES STRIPPED BUT NO PARBOX)

ExplSyntaxOff % BROUGHT OUTSIDE
% NO SPACES STRIPPED FROM THE FOLLOWING CONTENT (BUT NO PARBOX)
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument









share|improve this question
























  • You are missing the second argument of NewDocumentEnvironment ( the end code). And in case you are expecting that the environment; it will be inserted after the parbox.

    – Ulrike Fischer
    Jun 5 at 7:10






  • 3





    Side remark: dim_new:NmyDim inside an enviroment is wrong, you will get errors as soon as you use the environment the second time.

    – Ulrike Fischer
    Jun 5 at 7:56






  • 3





    It is well known that you cannot do newenvironmentfooparbox; nothing should make you think that it becomes possible with expl3, because it is impossible in TeX to have a brace unbalanced token list inside the argument to a macro.

    – egreg
    Jun 5 at 8:35

















2















I thought turning syntax on/off at appropriate places would make everything work, but I can't use ExplSyntaxOn/Off inside NewDocumentEnvironment so the following code sample doesn't work:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
ExplSyntaxOn
dim_new:NmyDim % DOESN'T WORK
ExplSyntaxOff


% END PARBOX

beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


So I have to enclose the environment within syntax switches, as follows, but then anything passed to the environment loses its spaces:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


% END PARBOX

ExplSyntaxOff % BROUGHT OUTSIDE
% SPACES OF THE FOLLOWING CONTENT ARE STRIPPED
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


How do I retain spaces of the passed content without replacing them with ~ symbols?



By the way, following doesn't strip spaces (but lacks parbox as I need it):



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

dim_new:NmyDim % WORKS (NO SPACES STRIPPED BUT NO PARBOX)

ExplSyntaxOff % BROUGHT OUTSIDE
% NO SPACES STRIPPED FROM THE FOLLOWING CONTENT (BUT NO PARBOX)
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument









share|improve this question
























  • You are missing the second argument of NewDocumentEnvironment ( the end code). And in case you are expecting that the environment; it will be inserted after the parbox.

    – Ulrike Fischer
    Jun 5 at 7:10






  • 3





    Side remark: dim_new:NmyDim inside an enviroment is wrong, you will get errors as soon as you use the environment the second time.

    – Ulrike Fischer
    Jun 5 at 7:56






  • 3





    It is well known that you cannot do newenvironmentfooparbox; nothing should make you think that it becomes possible with expl3, because it is impossible in TeX to have a brace unbalanced token list inside the argument to a macro.

    – egreg
    Jun 5 at 8:35













2












2








2








I thought turning syntax on/off at appropriate places would make everything work, but I can't use ExplSyntaxOn/Off inside NewDocumentEnvironment so the following code sample doesn't work:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
ExplSyntaxOn
dim_new:NmyDim % DOESN'T WORK
ExplSyntaxOff


% END PARBOX

beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


So I have to enclose the environment within syntax switches, as follows, but then anything passed to the environment loses its spaces:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


% END PARBOX

ExplSyntaxOff % BROUGHT OUTSIDE
% SPACES OF THE FOLLOWING CONTENT ARE STRIPPED
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


How do I retain spaces of the passed content without replacing them with ~ symbols?



By the way, following doesn't strip spaces (but lacks parbox as I need it):



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

dim_new:NmyDim % WORKS (NO SPACES STRIPPED BUT NO PARBOX)

ExplSyntaxOff % BROUGHT OUTSIDE
% NO SPACES STRIPPED FROM THE FOLLOWING CONTENT (BUT NO PARBOX)
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument









share|improve this question
















I thought turning syntax on/off at appropriate places would make everything work, but I can't use ExplSyntaxOn/Off inside NewDocumentEnvironment so the following code sample doesn't work:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
ExplSyntaxOn
dim_new:NmyDim % DOESN'T WORK
ExplSyntaxOff


% END PARBOX

beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


So I have to enclose the environment within syntax switches, as follows, but then anything passed to the environment loses its spaces:



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


% END PARBOX

ExplSyntaxOff % BROUGHT OUTSIDE
% SPACES OF THE FOLLOWING CONTENT ARE STRIPPED
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument


How do I retain spaces of the passed content without replacing them with ~ symbols?



By the way, following doesn't strip spaces (but lacks parbox as I need it):



documentclass[varwidth]standalone
usepackageexpl3
usepackagexparse
begindocument
ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

dim_new:NmyDim % WORKS (NO SPACES STRIPPED BUT NO PARBOX)

ExplSyntaxOff % BROUGHT OUTSIDE
% NO SPACES STRIPPED FROM THE FOLLOWING CONTENT (BUT NO PARBOX)
beginmyPar
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
endmyPar
enddocument






environments paragraphs expl3 latex3 xparse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 5 at 17:06







bp2017

















asked Jun 5 at 6:54









bp2017bp2017

1,371316




1,371316












  • You are missing the second argument of NewDocumentEnvironment ( the end code). And in case you are expecting that the environment; it will be inserted after the parbox.

    – Ulrike Fischer
    Jun 5 at 7:10






  • 3





    Side remark: dim_new:NmyDim inside an enviroment is wrong, you will get errors as soon as you use the environment the second time.

    – Ulrike Fischer
    Jun 5 at 7:56






  • 3





    It is well known that you cannot do newenvironmentfooparbox; nothing should make you think that it becomes possible with expl3, because it is impossible in TeX to have a brace unbalanced token list inside the argument to a macro.

    – egreg
    Jun 5 at 8:35

















  • You are missing the second argument of NewDocumentEnvironment ( the end code). And in case you are expecting that the environment; it will be inserted after the parbox.

    – Ulrike Fischer
    Jun 5 at 7:10






  • 3





    Side remark: dim_new:NmyDim inside an enviroment is wrong, you will get errors as soon as you use the environment the second time.

    – Ulrike Fischer
    Jun 5 at 7:56






  • 3





    It is well known that you cannot do newenvironmentfooparbox; nothing should make you think that it becomes possible with expl3, because it is impossible in TeX to have a brace unbalanced token list inside the argument to a macro.

    – egreg
    Jun 5 at 8:35
















You are missing the second argument of NewDocumentEnvironment ( the end code). And in case you are expecting that the environment; it will be inserted after the parbox.

– Ulrike Fischer
Jun 5 at 7:10





You are missing the second argument of NewDocumentEnvironment ( the end code). And in case you are expecting that the environment; it will be inserted after the parbox.

– Ulrike Fischer
Jun 5 at 7:10




3




3





Side remark: dim_new:NmyDim inside an enviroment is wrong, you will get errors as soon as you use the environment the second time.

– Ulrike Fischer
Jun 5 at 7:56





Side remark: dim_new:NmyDim inside an enviroment is wrong, you will get errors as soon as you use the environment the second time.

– Ulrike Fischer
Jun 5 at 7:56




3




3





It is well known that you cannot do newenvironmentfooparbox; nothing should make you think that it becomes possible with expl3, because it is impossible in TeX to have a brace unbalanced token list inside the argument to a macro.

– egreg
Jun 5 at 8:35





It is well known that you cannot do newenvironmentfooparbox; nothing should make you think that it becomes possible with expl3, because it is impossible in TeX to have a brace unbalanced token list inside the argument to a macro.

– egreg
Jun 5 at 8:35










3 Answers
3






active

oldest

votes


















6














You are tricking yourself with improper indentation. Remember that TeX does not care about indentation; any closing brace will end the group started by the last opening brace. Let's look at the environment definition in your second code snippet with the correct indentation (i.e. indentation that reflects how TeX reads your code).



ExplSyntaxOn % BROUGHT OUTSIDE
NewDocumentEnvironmentmyPar

parbox40mm
% START PARBOX
dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


% END PARBOX

ExplSyntaxOff % BROUGHT OUTSIDE


As you can see, your parbox ends right after the dim_new:N line, then there is an empty group and then the group of the environment start code ends. Importantly there is no environment end code before ExplSyntaxOff, which means that ExplSyntaxOff itself is taken as the environment end code.



Thus, expl3 syntax stays on, which causes all the spaces to be gobbled (among other havoc). This is not isolated to your environment. Add any text outside of beginmyPar .. endmypar and you will see the same problems. This is even true after the environment because the ExplSyntaxOff at the end of the environment is confined to the environment group and has no effect outside of it.



As Ulrike Fischer pointed out in her comment, using dim_new:NmyDim inside the environment is also problematic. You will get an error message telling you that myDim is already defined once you use the environment a second time. Define myDim once in the preamble and then only use it inside the environment.



You can avoid your grouping problem by using the b-type argument as demonstrated in Henri Menke's answer or just using a minipage instead of a parbox:



ExplSyntaxOn
NewDocumentEnvironmentmyPar
beginminipage40mm
% Don't do dim_new:NmyDim here!

endminipage

ExplSyntaxOff





share|improve this answer

























  • I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

    – frougon
    Jun 5 at 8:14












  • @frougon: Thanks, I added some remarks regarding the issue.

    – schtandard
    Jun 5 at 8:26











  • Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

    – frougon
    Jun 5 at 8:32











  • @frougon: You are right, thanks. Fixed.

    – schtandard
    Jun 5 at 8:36


















6














1. Unbalanced token lists



Here's what you're trying to do is, in traditional terms,



newenvironmentmyPar
%
parbox40mm% start parbox

%
% end parbox



This is wrong code to begin with. Written in a different way to better see what happens



newenvironmentmyParparbox40mm


This is unfinished, because it misses the third mandatory argument to newenvironment.



It is not possible to have a brace unbalanced token list in the argument to a macro. The } that you mean to balance the before parbox is instead matching the after parbox. Chaos ensues.



2. Internal syntax markers



Whoever wants to delve into LaTeX programming should know about tokenization. When input is absorbed by TeX, it is transformed into tokens: symbolic tokens (vulgo, commands) and character tokens. The latter have their category code permanently attached. So if you say



newcommandfoomakeatletter @makeatother


you get @ to have the category code it has when newcommand is performed. So if you do



newcommandfoomakeatletterdimen@=2p@makeatother


upon execution of foo you'll receive puzzling error messages, unless the definition is surrounded by makeatletter and makeatother itself. But then the ones inside the definition do nothing at all. See What do makeatletter and makeatother do? for more information.



It is exactly the same with ExplSyntaxOn and ExplSyntaxOff.



What happens in your second attempt, when bringing them outside the code for myPar is that, due to the mistake described in chapter 1, ExplSyntaxOff is taken as the “end part” of the myPar environment, so it is not executed and ExplSyntaxOn remains in force when reading the text following beginmyPar.



3. Miscellaneous



Doing dim_new:N as part of the environment is wrong no matter what. Doing newlength inside an environment would be wrong as well, although it doesn't raise errors. Let's see with an example



newenvironmentfoonewlengthfoolen


will dutifully create and waste a new skip register at each call of beginfoo. This is because the name assignment is local, but the register assignment is global as LaTeX doesn't want to take the risk of referring to the same register in different ways.



With expl3 the things are a bit different: with



NewDocumentEnvironmentfoo
dim_new:N myDim



(properly surrounded by ExplSyntaxOn and ExplSyntaxOff), also the name assignment is global. Thus upon calling beginfoo a second time LaTeX will raise an error.



4. How to make a parbox with an environment?



Use minipage, of course.






share|improve this answer






























    4














    Use the new b-type argument which captures the body of an environment for use like a regular argument #1. The +b allows multiple paragraphs.



    documentclass[varwidth]standalone
    usepackagexparse

    ExplSyntaxOn

    NewDocumentEnvironment myPar +b

    parbox 40mm

    dim_new:N myDim
    #1



    ExplSyntaxOff

    begindocument
    beginmyPar
    Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
    endmyPar
    enddocument


    enter image description here






    share|improve this answer























    • you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

      – David Carlisle
      Jun 5 at 8:11











    • @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

      – Henri Menke
      Jun 5 at 8:57











    • sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

      – David Carlisle
      Jun 5 at 9:36













    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%2f494265%2fwhy-newdocumentenvironment-within-explsyntaxon-off-removes-spaces-from-content%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You are tricking yourself with improper indentation. Remember that TeX does not care about indentation; any closing brace will end the group started by the last opening brace. Let's look at the environment definition in your second code snippet with the correct indentation (i.e. indentation that reflects how TeX reads your code).



    ExplSyntaxOn % BROUGHT OUTSIDE
    NewDocumentEnvironmentmyPar

    parbox40mm
    % START PARBOX
    dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


    % END PARBOX

    ExplSyntaxOff % BROUGHT OUTSIDE


    As you can see, your parbox ends right after the dim_new:N line, then there is an empty group and then the group of the environment start code ends. Importantly there is no environment end code before ExplSyntaxOff, which means that ExplSyntaxOff itself is taken as the environment end code.



    Thus, expl3 syntax stays on, which causes all the spaces to be gobbled (among other havoc). This is not isolated to your environment. Add any text outside of beginmyPar .. endmypar and you will see the same problems. This is even true after the environment because the ExplSyntaxOff at the end of the environment is confined to the environment group and has no effect outside of it.



    As Ulrike Fischer pointed out in her comment, using dim_new:NmyDim inside the environment is also problematic. You will get an error message telling you that myDim is already defined once you use the environment a second time. Define myDim once in the preamble and then only use it inside the environment.



    You can avoid your grouping problem by using the b-type argument as demonstrated in Henri Menke's answer or just using a minipage instead of a parbox:



    ExplSyntaxOn
    NewDocumentEnvironmentmyPar
    beginminipage40mm
    % Don't do dim_new:NmyDim here!

    endminipage

    ExplSyntaxOff





    share|improve this answer

























    • I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

      – frougon
      Jun 5 at 8:14












    • @frougon: Thanks, I added some remarks regarding the issue.

      – schtandard
      Jun 5 at 8:26











    • Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

      – frougon
      Jun 5 at 8:32











    • @frougon: You are right, thanks. Fixed.

      – schtandard
      Jun 5 at 8:36















    6














    You are tricking yourself with improper indentation. Remember that TeX does not care about indentation; any closing brace will end the group started by the last opening brace. Let's look at the environment definition in your second code snippet with the correct indentation (i.e. indentation that reflects how TeX reads your code).



    ExplSyntaxOn % BROUGHT OUTSIDE
    NewDocumentEnvironmentmyPar

    parbox40mm
    % START PARBOX
    dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


    % END PARBOX

    ExplSyntaxOff % BROUGHT OUTSIDE


    As you can see, your parbox ends right after the dim_new:N line, then there is an empty group and then the group of the environment start code ends. Importantly there is no environment end code before ExplSyntaxOff, which means that ExplSyntaxOff itself is taken as the environment end code.



    Thus, expl3 syntax stays on, which causes all the spaces to be gobbled (among other havoc). This is not isolated to your environment. Add any text outside of beginmyPar .. endmypar and you will see the same problems. This is even true after the environment because the ExplSyntaxOff at the end of the environment is confined to the environment group and has no effect outside of it.



    As Ulrike Fischer pointed out in her comment, using dim_new:NmyDim inside the environment is also problematic. You will get an error message telling you that myDim is already defined once you use the environment a second time. Define myDim once in the preamble and then only use it inside the environment.



    You can avoid your grouping problem by using the b-type argument as demonstrated in Henri Menke's answer or just using a minipage instead of a parbox:



    ExplSyntaxOn
    NewDocumentEnvironmentmyPar
    beginminipage40mm
    % Don't do dim_new:NmyDim here!

    endminipage

    ExplSyntaxOff





    share|improve this answer

























    • I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

      – frougon
      Jun 5 at 8:14












    • @frougon: Thanks, I added some remarks regarding the issue.

      – schtandard
      Jun 5 at 8:26











    • Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

      – frougon
      Jun 5 at 8:32











    • @frougon: You are right, thanks. Fixed.

      – schtandard
      Jun 5 at 8:36













    6












    6








    6







    You are tricking yourself with improper indentation. Remember that TeX does not care about indentation; any closing brace will end the group started by the last opening brace. Let's look at the environment definition in your second code snippet with the correct indentation (i.e. indentation that reflects how TeX reads your code).



    ExplSyntaxOn % BROUGHT OUTSIDE
    NewDocumentEnvironmentmyPar

    parbox40mm
    % START PARBOX
    dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


    % END PARBOX

    ExplSyntaxOff % BROUGHT OUTSIDE


    As you can see, your parbox ends right after the dim_new:N line, then there is an empty group and then the group of the environment start code ends. Importantly there is no environment end code before ExplSyntaxOff, which means that ExplSyntaxOff itself is taken as the environment end code.



    Thus, expl3 syntax stays on, which causes all the spaces to be gobbled (among other havoc). This is not isolated to your environment. Add any text outside of beginmyPar .. endmypar and you will see the same problems. This is even true after the environment because the ExplSyntaxOff at the end of the environment is confined to the environment group and has no effect outside of it.



    As Ulrike Fischer pointed out in her comment, using dim_new:NmyDim inside the environment is also problematic. You will get an error message telling you that myDim is already defined once you use the environment a second time. Define myDim once in the preamble and then only use it inside the environment.



    You can avoid your grouping problem by using the b-type argument as demonstrated in Henri Menke's answer or just using a minipage instead of a parbox:



    ExplSyntaxOn
    NewDocumentEnvironmentmyPar
    beginminipage40mm
    % Don't do dim_new:NmyDim here!

    endminipage

    ExplSyntaxOff





    share|improve this answer















    You are tricking yourself with improper indentation. Remember that TeX does not care about indentation; any closing brace will end the group started by the last opening brace. Let's look at the environment definition in your second code snippet with the correct indentation (i.e. indentation that reflects how TeX reads your code).



    ExplSyntaxOn % BROUGHT OUTSIDE
    NewDocumentEnvironmentmyPar

    parbox40mm
    % START PARBOX
    dim_new:NmyDim % WORKS (BUT NO SPACES REMAIN)


    % END PARBOX

    ExplSyntaxOff % BROUGHT OUTSIDE


    As you can see, your parbox ends right after the dim_new:N line, then there is an empty group and then the group of the environment start code ends. Importantly there is no environment end code before ExplSyntaxOff, which means that ExplSyntaxOff itself is taken as the environment end code.



    Thus, expl3 syntax stays on, which causes all the spaces to be gobbled (among other havoc). This is not isolated to your environment. Add any text outside of beginmyPar .. endmypar and you will see the same problems. This is even true after the environment because the ExplSyntaxOff at the end of the environment is confined to the environment group and has no effect outside of it.



    As Ulrike Fischer pointed out in her comment, using dim_new:NmyDim inside the environment is also problematic. You will get an error message telling you that myDim is already defined once you use the environment a second time. Define myDim once in the preamble and then only use it inside the environment.



    You can avoid your grouping problem by using the b-type argument as demonstrated in Henri Menke's answer or just using a minipage instead of a parbox:



    ExplSyntaxOn
    NewDocumentEnvironmentmyPar
    beginminipage40mm
    % Don't do dim_new:NmyDim here!

    endminipage

    ExplSyntaxOff






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 5 at 8:36

























    answered Jun 5 at 7:52









    schtandardschtandard

    3,71311228




    3,71311228












    • I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

      – frougon
      Jun 5 at 8:14












    • @frougon: Thanks, I added some remarks regarding the issue.

      – schtandard
      Jun 5 at 8:26











    • Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

      – frougon
      Jun 5 at 8:32











    • @frougon: You are right, thanks. Fixed.

      – schtandard
      Jun 5 at 8:36

















    • I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

      – frougon
      Jun 5 at 8:14












    • @frougon: Thanks, I added some remarks regarding the issue.

      – schtandard
      Jun 5 at 8:26











    • Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

      – frougon
      Jun 5 at 8:32











    • @frougon: You are right, thanks. Fixed.

      – schtandard
      Jun 5 at 8:36
















    I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

    – frougon
    Jun 5 at 8:14






    I upvoted because I think you explained well the most important issues, but there is also the misplaced dim_new:N pointed out by Ulrike Fischer. :-) (It would probably also be worthwhile to explain why ExplSyntaxOn and ExplSyntaxOff aren't useful inside a command or environment definition in general.)

    – frougon
    Jun 5 at 8:14














    @frougon: Thanks, I added some remarks regarding the issue.

    – schtandard
    Jun 5 at 8:26





    @frougon: Thanks, I added some remarks regarding the issue.

    – schtandard
    Jun 5 at 8:26













    Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

    – frougon
    Jun 5 at 8:32





    Thanks, this sounds good to me. Other little thing: the “group only containing a space (the line break)” is actually empty because of the ExplSyntaxOn before NewDocumentEnvironment (a correctly-placed ExplSyntaxOn).

    – frougon
    Jun 5 at 8:32













    @frougon: You are right, thanks. Fixed.

    – schtandard
    Jun 5 at 8:36





    @frougon: You are right, thanks. Fixed.

    – schtandard
    Jun 5 at 8:36













    6














    1. Unbalanced token lists



    Here's what you're trying to do is, in traditional terms,



    newenvironmentmyPar
    %
    parbox40mm% start parbox

    %
    % end parbox



    This is wrong code to begin with. Written in a different way to better see what happens



    newenvironmentmyParparbox40mm


    This is unfinished, because it misses the third mandatory argument to newenvironment.



    It is not possible to have a brace unbalanced token list in the argument to a macro. The } that you mean to balance the before parbox is instead matching the after parbox. Chaos ensues.



    2. Internal syntax markers



    Whoever wants to delve into LaTeX programming should know about tokenization. When input is absorbed by TeX, it is transformed into tokens: symbolic tokens (vulgo, commands) and character tokens. The latter have their category code permanently attached. So if you say



    newcommandfoomakeatletter @makeatother


    you get @ to have the category code it has when newcommand is performed. So if you do



    newcommandfoomakeatletterdimen@=2p@makeatother


    upon execution of foo you'll receive puzzling error messages, unless the definition is surrounded by makeatletter and makeatother itself. But then the ones inside the definition do nothing at all. See What do makeatletter and makeatother do? for more information.



    It is exactly the same with ExplSyntaxOn and ExplSyntaxOff.



    What happens in your second attempt, when bringing them outside the code for myPar is that, due to the mistake described in chapter 1, ExplSyntaxOff is taken as the “end part” of the myPar environment, so it is not executed and ExplSyntaxOn remains in force when reading the text following beginmyPar.



    3. Miscellaneous



    Doing dim_new:N as part of the environment is wrong no matter what. Doing newlength inside an environment would be wrong as well, although it doesn't raise errors. Let's see with an example



    newenvironmentfoonewlengthfoolen


    will dutifully create and waste a new skip register at each call of beginfoo. This is because the name assignment is local, but the register assignment is global as LaTeX doesn't want to take the risk of referring to the same register in different ways.



    With expl3 the things are a bit different: with



    NewDocumentEnvironmentfoo
    dim_new:N myDim



    (properly surrounded by ExplSyntaxOn and ExplSyntaxOff), also the name assignment is global. Thus upon calling beginfoo a second time LaTeX will raise an error.



    4. How to make a parbox with an environment?



    Use minipage, of course.






    share|improve this answer



























      6














      1. Unbalanced token lists



      Here's what you're trying to do is, in traditional terms,



      newenvironmentmyPar
      %
      parbox40mm% start parbox

      %
      % end parbox



      This is wrong code to begin with. Written in a different way to better see what happens



      newenvironmentmyParparbox40mm


      This is unfinished, because it misses the third mandatory argument to newenvironment.



      It is not possible to have a brace unbalanced token list in the argument to a macro. The } that you mean to balance the before parbox is instead matching the after parbox. Chaos ensues.



      2. Internal syntax markers



      Whoever wants to delve into LaTeX programming should know about tokenization. When input is absorbed by TeX, it is transformed into tokens: symbolic tokens (vulgo, commands) and character tokens. The latter have their category code permanently attached. So if you say



      newcommandfoomakeatletter @makeatother


      you get @ to have the category code it has when newcommand is performed. So if you do



      newcommandfoomakeatletterdimen@=2p@makeatother


      upon execution of foo you'll receive puzzling error messages, unless the definition is surrounded by makeatletter and makeatother itself. But then the ones inside the definition do nothing at all. See What do makeatletter and makeatother do? for more information.



      It is exactly the same with ExplSyntaxOn and ExplSyntaxOff.



      What happens in your second attempt, when bringing them outside the code for myPar is that, due to the mistake described in chapter 1, ExplSyntaxOff is taken as the “end part” of the myPar environment, so it is not executed and ExplSyntaxOn remains in force when reading the text following beginmyPar.



      3. Miscellaneous



      Doing dim_new:N as part of the environment is wrong no matter what. Doing newlength inside an environment would be wrong as well, although it doesn't raise errors. Let's see with an example



      newenvironmentfoonewlengthfoolen


      will dutifully create and waste a new skip register at each call of beginfoo. This is because the name assignment is local, but the register assignment is global as LaTeX doesn't want to take the risk of referring to the same register in different ways.



      With expl3 the things are a bit different: with



      NewDocumentEnvironmentfoo
      dim_new:N myDim



      (properly surrounded by ExplSyntaxOn and ExplSyntaxOff), also the name assignment is global. Thus upon calling beginfoo a second time LaTeX will raise an error.



      4. How to make a parbox with an environment?



      Use minipage, of course.






      share|improve this answer

























        6












        6








        6







        1. Unbalanced token lists



        Here's what you're trying to do is, in traditional terms,



        newenvironmentmyPar
        %
        parbox40mm% start parbox

        %
        % end parbox



        This is wrong code to begin with. Written in a different way to better see what happens



        newenvironmentmyParparbox40mm


        This is unfinished, because it misses the third mandatory argument to newenvironment.



        It is not possible to have a brace unbalanced token list in the argument to a macro. The } that you mean to balance the before parbox is instead matching the after parbox. Chaos ensues.



        2. Internal syntax markers



        Whoever wants to delve into LaTeX programming should know about tokenization. When input is absorbed by TeX, it is transformed into tokens: symbolic tokens (vulgo, commands) and character tokens. The latter have their category code permanently attached. So if you say



        newcommandfoomakeatletter @makeatother


        you get @ to have the category code it has when newcommand is performed. So if you do



        newcommandfoomakeatletterdimen@=2p@makeatother


        upon execution of foo you'll receive puzzling error messages, unless the definition is surrounded by makeatletter and makeatother itself. But then the ones inside the definition do nothing at all. See What do makeatletter and makeatother do? for more information.



        It is exactly the same with ExplSyntaxOn and ExplSyntaxOff.



        What happens in your second attempt, when bringing them outside the code for myPar is that, due to the mistake described in chapter 1, ExplSyntaxOff is taken as the “end part” of the myPar environment, so it is not executed and ExplSyntaxOn remains in force when reading the text following beginmyPar.



        3. Miscellaneous



        Doing dim_new:N as part of the environment is wrong no matter what. Doing newlength inside an environment would be wrong as well, although it doesn't raise errors. Let's see with an example



        newenvironmentfoonewlengthfoolen


        will dutifully create and waste a new skip register at each call of beginfoo. This is because the name assignment is local, but the register assignment is global as LaTeX doesn't want to take the risk of referring to the same register in different ways.



        With expl3 the things are a bit different: with



        NewDocumentEnvironmentfoo
        dim_new:N myDim



        (properly surrounded by ExplSyntaxOn and ExplSyntaxOff), also the name assignment is global. Thus upon calling beginfoo a second time LaTeX will raise an error.



        4. How to make a parbox with an environment?



        Use minipage, of course.






        share|improve this answer













        1. Unbalanced token lists



        Here's what you're trying to do is, in traditional terms,



        newenvironmentmyPar
        %
        parbox40mm% start parbox

        %
        % end parbox



        This is wrong code to begin with. Written in a different way to better see what happens



        newenvironmentmyParparbox40mm


        This is unfinished, because it misses the third mandatory argument to newenvironment.



        It is not possible to have a brace unbalanced token list in the argument to a macro. The } that you mean to balance the before parbox is instead matching the after parbox. Chaos ensues.



        2. Internal syntax markers



        Whoever wants to delve into LaTeX programming should know about tokenization. When input is absorbed by TeX, it is transformed into tokens: symbolic tokens (vulgo, commands) and character tokens. The latter have their category code permanently attached. So if you say



        newcommandfoomakeatletter @makeatother


        you get @ to have the category code it has when newcommand is performed. So if you do



        newcommandfoomakeatletterdimen@=2p@makeatother


        upon execution of foo you'll receive puzzling error messages, unless the definition is surrounded by makeatletter and makeatother itself. But then the ones inside the definition do nothing at all. See What do makeatletter and makeatother do? for more information.



        It is exactly the same with ExplSyntaxOn and ExplSyntaxOff.



        What happens in your second attempt, when bringing them outside the code for myPar is that, due to the mistake described in chapter 1, ExplSyntaxOff is taken as the “end part” of the myPar environment, so it is not executed and ExplSyntaxOn remains in force when reading the text following beginmyPar.



        3. Miscellaneous



        Doing dim_new:N as part of the environment is wrong no matter what. Doing newlength inside an environment would be wrong as well, although it doesn't raise errors. Let's see with an example



        newenvironmentfoonewlengthfoolen


        will dutifully create and waste a new skip register at each call of beginfoo. This is because the name assignment is local, but the register assignment is global as LaTeX doesn't want to take the risk of referring to the same register in different ways.



        With expl3 the things are a bit different: with



        NewDocumentEnvironmentfoo
        dim_new:N myDim



        (properly surrounded by ExplSyntaxOn and ExplSyntaxOff), also the name assignment is global. Thus upon calling beginfoo a second time LaTeX will raise an error.



        4. How to make a parbox with an environment?



        Use minipage, of course.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 5 at 9:09









        egregegreg

        749k8919623303




        749k8919623303





















            4














            Use the new b-type argument which captures the body of an environment for use like a regular argument #1. The +b allows multiple paragraphs.



            documentclass[varwidth]standalone
            usepackagexparse

            ExplSyntaxOn

            NewDocumentEnvironment myPar +b

            parbox 40mm

            dim_new:N myDim
            #1



            ExplSyntaxOff

            begindocument
            beginmyPar
            Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
            endmyPar
            enddocument


            enter image description here






            share|improve this answer























            • you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

              – David Carlisle
              Jun 5 at 8:11











            • @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

              – Henri Menke
              Jun 5 at 8:57











            • sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

              – David Carlisle
              Jun 5 at 9:36















            4














            Use the new b-type argument which captures the body of an environment for use like a regular argument #1. The +b allows multiple paragraphs.



            documentclass[varwidth]standalone
            usepackagexparse

            ExplSyntaxOn

            NewDocumentEnvironment myPar +b

            parbox 40mm

            dim_new:N myDim
            #1



            ExplSyntaxOff

            begindocument
            beginmyPar
            Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
            endmyPar
            enddocument


            enter image description here






            share|improve this answer























            • you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

              – David Carlisle
              Jun 5 at 8:11











            • @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

              – Henri Menke
              Jun 5 at 8:57











            • sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

              – David Carlisle
              Jun 5 at 9:36













            4












            4








            4







            Use the new b-type argument which captures the body of an environment for use like a regular argument #1. The +b allows multiple paragraphs.



            documentclass[varwidth]standalone
            usepackagexparse

            ExplSyntaxOn

            NewDocumentEnvironment myPar +b

            parbox 40mm

            dim_new:N myDim
            #1



            ExplSyntaxOff

            begindocument
            beginmyPar
            Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
            endmyPar
            enddocument


            enter image description here






            share|improve this answer













            Use the new b-type argument which captures the body of an environment for use like a regular argument #1. The +b allows multiple paragraphs.



            documentclass[varwidth]standalone
            usepackagexparse

            ExplSyntaxOn

            NewDocumentEnvironment myPar +b

            parbox 40mm

            dim_new:N myDim
            #1



            ExplSyntaxOff

            begindocument
            beginmyPar
            Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
            endmyPar
            enddocument


            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 5 at 7:52









            Henri MenkeHenri Menke

            79.9k9176291




            79.9k9176291












            • you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

              – David Carlisle
              Jun 5 at 8:11











            • @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

              – Henri Menke
              Jun 5 at 8:57











            • sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

              – David Carlisle
              Jun 5 at 9:36

















            • you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

              – David Carlisle
              Jun 5 at 8:11











            • @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

              – Henri Menke
              Jun 5 at 8:57











            • sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

              – David Carlisle
              Jun 5 at 9:36
















            you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

            – David Carlisle
            Jun 5 at 8:11





            you fixed the catcode issues but dim_new inside the environment means it can only be called once, which is probably not the intended definition.....

            – David Carlisle
            Jun 5 at 8:11













            @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

            – Henri Menke
            Jun 5 at 8:57





            @DavidCarlisle That I just copied from the OP. I have no idea what the intention is, but you are of course right.

            – Henri Menke
            Jun 5 at 8:57













            sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

            – David Carlisle
            Jun 5 at 9:36





            sure, not suggesting you fix it but left a comment for future readers who may copy code from answers......

            – David Carlisle
            Jun 5 at 9:36

















            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%2f494265%2fwhy-newdocumentenvironment-within-explsyntaxon-off-removes-spaces-from-content%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 write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

            What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

            Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?