How can I check if the current code is inside a certain environment?How can I check if the current code is inside a tikzpicture?Detecting if inside a tikzpictureHow to test if I'm currently in a footnote or notHow can I center arbitrary content between two horizontal lines?Checking environment in order to end itHow to access name of environment in its own definitionpassing options to the newenvironmentUse column-separator & (ampersand) inside newenvironmentHow can I create a customized environment?Environments with matched parens in begin and endAllow only certain commands in own environmentCompiler Error when Creating a Macro/EnvironmentHow can I know if I am inside an equation environment if it's nested with others?Symbol at the end of an environmentHow to delete the contents of the current paragraph and “re-start” from the beginning?Using a split environment inside every equation environment

Meta programming: Declare a new struct on the fly

Issues with Installing Powershell to Kali Machine

A social experiment. What is the worst that can happen?

Freedom of speech and where it applies

Why isn't KTEX's runway designation 10/28 instead of 9/27?

Invariance of results when scaling explanatory variables in logistic regression, is there a proof?

I2C signal and power over long range (10meter cable)

Is there a smaller tautogram checker?

Pronouncing Homer as in modern Greek

Is there a problem with hiding "forgot password" until it's needed?

Simulating a probability of 1 of 2^N with less than N random bits

For airliners, what prevents wing strikes on landing in bad weather?

Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

What will be the benefits of Brexit?

Is it possible to build a CPA Secure encryption scheme which remains secure even when the encryption of secret key is given?

Is there an Impartial Brexit Deal comparison site?

Perfect riffle shuffles

Visiting the UK as unmarried couple

Superhero words!

Can the electrostatic force be infinite in magnitude?

You're three for three

Could solar power be utilized and substitute coal in the 19th century?

Have I saved too much for retirement so far?



How can I check if the current code is inside a certain environment?


How can I check if the current code is inside a tikzpicture?Detecting if inside a tikzpictureHow to test if I'm currently in a footnote or notHow can I center arbitrary content between two horizontal lines?Checking environment in order to end itHow to access name of environment in its own definitionpassing options to the newenvironmentUse column-separator & (ampersand) inside newenvironmentHow can I create a customized environment?Environments with matched parens in begin and endAllow only certain commands in own environmentCompiler Error when Creating a Macro/EnvironmentHow can I know if I am inside an equation environment if it's nested with others?Symbol at the end of an environmentHow to delete the contents of the current paragraph and “re-start” from the beginning?Using a split environment inside every equation environment













20















I would like to define a command which checks if it is executed inside a certain environment. Like this:



documentclassarticle

newenvironmentmyenv[0]at start at end

newcommandinner[0]%
% if iside a myenv
(inner)
% else
beginmyenv
(inner)%
endmyenv
% end


begindocument

beginmyenv
inner
endmyenv

inner

enddocument


See the definition of inner. Is an if-condition of this form possible?










share|improve this question
























  • Related Question: Detecting if inside a tikzpicture

    – Peter Grill
    Nov 7 '18 at 23:30















20















I would like to define a command which checks if it is executed inside a certain environment. Like this:



documentclassarticle

newenvironmentmyenv[0]at start at end

newcommandinner[0]%
% if iside a myenv
(inner)
% else
beginmyenv
(inner)%
endmyenv
% end


begindocument

beginmyenv
inner
endmyenv

inner

enddocument


See the definition of inner. Is an if-condition of this form possible?










share|improve this question
























  • Related Question: Detecting if inside a tikzpicture

    – Peter Grill
    Nov 7 '18 at 23:30













20












20








20


6






I would like to define a command which checks if it is executed inside a certain environment. Like this:



documentclassarticle

newenvironmentmyenv[0]at start at end

newcommandinner[0]%
% if iside a myenv
(inner)
% else
beginmyenv
(inner)%
endmyenv
% end


begindocument

beginmyenv
inner
endmyenv

inner

enddocument


See the definition of inner. Is an if-condition of this form possible?










share|improve this question
















I would like to define a command which checks if it is executed inside a certain environment. Like this:



documentclassarticle

newenvironmentmyenv[0]at start at end

newcommandinner[0]%
% if iside a myenv
(inner)
% else
beginmyenv
(inner)%
endmyenv
% end


begindocument

beginmyenv
inner
endmyenv

inner

enddocument


See the definition of inner. Is an if-condition of this form possible?







environments conditionals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 20 '11 at 14:15









lockstep

192k53593723




192k53593723










asked May 20 '11 at 8:17









Nickolay KolevNickolay Kolev

1,70641731




1,70641731












  • Related Question: Detecting if inside a tikzpicture

    – Peter Grill
    Nov 7 '18 at 23:30

















  • Related Question: Detecting if inside a tikzpicture

    – Peter Grill
    Nov 7 '18 at 23:30
















Related Question: Detecting if inside a tikzpicture

– Peter Grill
Nov 7 '18 at 23:30





Related Question: Detecting if inside a tikzpicture

– Peter Grill
Nov 7 '18 at 23:30










2 Answers
2






active

oldest

votes


















20














LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommandinner%
ifx@currenvir@myenvname
(inner)
else
beginmyenv(inner)endmyenv
fi
newcommand*@myenvnamemyenv
makeatother


Another approach would be to define a global conditional that's set to true by myenv start code and to false by myenv end code. It depends mostly on what you are expecting from the myenv environment: can it appear nested inside itself?






share|improve this answer




















  • 2





    @Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

    – Martin Scharrer
    May 20 '11 at 10:28











  • This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

    – Danny Hansen
    15 mins ago











  • @DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

    – egreg
    6 mins ago


















4














If you use pdftex, etex or xetex (I mean engine), you can define a fully expandable test:



makeatletter defIfEnvir #1%
% implicit #2 "what if true"
% implicit #3 "what if false"
%
ifnum strcmp@currenvir#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi


makeatother


Such a conditional is defined in the gmutils package: @ifenvir, but there it's protected for some reason.






share|improve this answer

























  • If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

    – Natror
    May 20 '11 at 9:06











  • Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

    – Martin Scharrer
    May 20 '11 at 9:39











  • the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

    – egreg
    May 20 '11 at 10:04










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%2f18652%2fhow-can-i-check-if-the-current-code-is-inside-a-certain-environment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









20














LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommandinner%
ifx@currenvir@myenvname
(inner)
else
beginmyenv(inner)endmyenv
fi
newcommand*@myenvnamemyenv
makeatother


Another approach would be to define a global conditional that's set to true by myenv start code and to false by myenv end code. It depends mostly on what you are expecting from the myenv environment: can it appear nested inside itself?






share|improve this answer




















  • 2





    @Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

    – Martin Scharrer
    May 20 '11 at 10:28











  • This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

    – Danny Hansen
    15 mins ago











  • @DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

    – egreg
    6 mins ago















20














LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommandinner%
ifx@currenvir@myenvname
(inner)
else
beginmyenv(inner)endmyenv
fi
newcommand*@myenvnamemyenv
makeatother


Another approach would be to define a global conditional that's set to true by myenv start code and to false by myenv end code. It depends mostly on what you are expecting from the myenv environment: can it appear nested inside itself?






share|improve this answer




















  • 2





    @Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

    – Martin Scharrer
    May 20 '11 at 10:28











  • This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

    – Danny Hansen
    15 mins ago











  • @DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

    – egreg
    6 mins ago













20












20








20







LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommandinner%
ifx@currenvir@myenvname
(inner)
else
beginmyenv(inner)endmyenv
fi
newcommand*@myenvnamemyenv
makeatother


Another approach would be to define a global conditional that's set to true by myenv start code and to false by myenv end code. It depends mostly on what you are expecting from the myenv environment: can it appear nested inside itself?






share|improve this answer















LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommandinner%
ifx@currenvir@myenvname
(inner)
else
beginmyenv(inner)endmyenv
fi
newcommand*@myenvnamemyenv
makeatother


Another approach would be to define a global conditional that's set to true by myenv start code and to false by myenv end code. It depends mostly on what you are expecting from the myenv environment: can it appear nested inside itself?







share|improve this answer














share|improve this answer



share|improve this answer








edited 6 mins ago

























answered May 20 '11 at 8:32









egregegreg

729k8819263235




729k8819263235







  • 2





    @Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

    – Martin Scharrer
    May 20 '11 at 10:28











  • This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

    – Danny Hansen
    15 mins ago











  • @DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

    – egreg
    6 mins ago












  • 2





    @Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

    – Martin Scharrer
    May 20 '11 at 10:28











  • This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

    – Danny Hansen
    15 mins ago











  • @DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

    – egreg
    6 mins ago







2




2





@Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

– Martin Scharrer
May 20 '11 at 10:28





@Nickoley: Note that this doesn't work any longer when inner is used inside another environment inside the myenv environment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then @currenvir` will be center when inner is expanded.

– Martin Scharrer
May 20 '11 at 10:28













This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

– Danny Hansen
15 mins ago





This does not seem to work on my end. It produces the "else" output both inside and outside a myenv environment. However, both @currenvir and @myenvname display the correct (and the same) output when placed inside the environment.

– Danny Hansen
15 mins ago













@DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

– egreg
6 mins ago





@DannyHansen Right: in some update during the last eight years, the LaTeX kernel changed @currenvir to not be long as it used to be. Fixed by adding a *.

– egreg
6 mins ago











4














If you use pdftex, etex or xetex (I mean engine), you can define a fully expandable test:



makeatletter defIfEnvir #1%
% implicit #2 "what if true"
% implicit #3 "what if false"
%
ifnum strcmp@currenvir#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi


makeatother


Such a conditional is defined in the gmutils package: @ifenvir, but there it's protected for some reason.






share|improve this answer

























  • If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

    – Natror
    May 20 '11 at 9:06











  • Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

    – Martin Scharrer
    May 20 '11 at 9:39











  • the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

    – egreg
    May 20 '11 at 10:04















4














If you use pdftex, etex or xetex (I mean engine), you can define a fully expandable test:



makeatletter defIfEnvir #1%
% implicit #2 "what if true"
% implicit #3 "what if false"
%
ifnum strcmp@currenvir#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi


makeatother


Such a conditional is defined in the gmutils package: @ifenvir, but there it's protected for some reason.






share|improve this answer

























  • If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

    – Natror
    May 20 '11 at 9:06











  • Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

    – Martin Scharrer
    May 20 '11 at 9:39











  • the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

    – egreg
    May 20 '11 at 10:04













4












4








4







If you use pdftex, etex or xetex (I mean engine), you can define a fully expandable test:



makeatletter defIfEnvir #1%
% implicit #2 "what if true"
% implicit #3 "what if false"
%
ifnum strcmp@currenvir#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi


makeatother


Such a conditional is defined in the gmutils package: @ifenvir, but there it's protected for some reason.






share|improve this answer















If you use pdftex, etex or xetex (I mean engine), you can define a fully expandable test:



makeatletter defIfEnvir #1%
% implicit #2 "what if true"
% implicit #3 "what if false"
%
ifnum strcmp@currenvir#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi


makeatother


Such a conditional is defined in the gmutils package: @ifenvir, but there it's protected for some reason.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 20 '11 at 9:40









Martin Scharrer

203k47651825




203k47651825










answered May 20 '11 at 9:01









NatrorNatror

612




612












  • If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

    – Natror
    May 20 '11 at 9:06











  • Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

    – Martin Scharrer
    May 20 '11 at 9:39











  • the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

    – egreg
    May 20 '11 at 10:04

















  • If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

    – Natror
    May 20 '11 at 9:06











  • Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

    – Martin Scharrer
    May 20 '11 at 9:39











  • the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

    – egreg
    May 20 '11 at 10:04
















If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

– Natror
May 20 '11 at 9:06





If you prefer the if… … else … fi form, just replace expandafter@first/secondoftwo with your stuff, but i'd suggest the arguments-form because it's robust to unbalanced if's.

– Natror
May 20 '11 at 9:06













Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

– Martin Scharrer
May 20 '11 at 9:39





Please indent your code with four spaces (or use the '101010' button to do this) so that it is properly highlighted. Please don't use HTML for code formatting. Thanks.

– Martin Scharrer
May 20 '11 at 9:39













the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

– egreg
May 20 '11 at 10:04





the command is strcmp in XeTeX and pdfstrcmp in pdfTeX. One can load pdftexcmds and use pdf@strcmp in all engines (including LuaTeX).

– egreg
May 20 '11 at 10:04

















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%2f18652%2fhow-can-i-check-if-the-current-code-is-inside-a-certain-environment%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

Isabella Eugénie Boyer Biographie | Références | Menu de navigationmodifiermodifier le codeComparator to Compute the Relative Value of a U.S. Dollar Amount – 1774 to Present.

Join wedge with single bond in chemfigHow to make only one part of double bond bold with chemfig?Crossing bonds in chemfigjoining atoms in chemfig. Two adjacent molculesHow do I selectively change bond length in chemfig?Ugly bond joints in chemfigchemfig: reaction above arrowUsing the mhchem and chemfig packages in conjunctionBonding to specific element letter using chemfigResonance hybrids in chemfigScale chemfig molecule in beamer with tikzWhy does this chemfig bond with a hook start in the middle of the atom?

Are small insurances worth itIs insurance worth it if you can afford to replace the item? If not, when is it?Is accident insurance worth it for my kids who play sportsIs insuring property for more than it is worth allowed?At what point does it become worth it to file an insurance claim?Are wage loss insurance programs worth the cost compared to having an emergency fund?When is an event worth insuring against?Is insurance worth it if you can afford to replace the item? If not, when is it?FHA loan just commenced : Any way to get any of the up-front mortgage insurance back?Which types of insurances do I need to buy?Should I carry less renter's insurance if I can self-insure?Mortgage Adviser Signed Me Up For Multiple Home and Life Insurances (UK)Why many travel insurances don't cover country of nationality?