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

Identify a stage play about a VR experience in which participants are encouraged to simulate performing horrific activities

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

How to prevent YouTube from showing already watched videos?

Can a malicious addon access internet history and such in chrome/firefox?

Calculating the number of days between 2 dates in Excel

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

Why does this part of the Space Shuttle launch pad seem to be floating in air?

Is it possible to have a strip of cold climate in the middle of a planet?

Teaching indefinite integrals that require special-casing

Modern Day Chaucer

Can the electrostatic force be infinite in magnitude?

Is there any significance to the Valyrian Stone vault door of Qarth?

How to be able to process a large JSON response?

How do I repair my stair bannister?

The most efficient algorithm to find all possible integer pairs which sum to a given integer

Freedom of speech and where it applies

Giant Toughroad SLR 2 for 200 miles in two days, will it make it?

Why is delta-v is the most useful quantity for planning space travel?

A known event to a history junkie

Can a Gentile theist be saved?

What if somebody invests in my application?

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

Java - What do constructor type arguments mean when placed *before* the type?

Resetting two CD4017 counters simultaneously, only one resets



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
    29 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
    20 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
    29 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
    20 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
    29 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
    20 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 20 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
    29 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
    20 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
    29 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
    20 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
29 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
29 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
20 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
20 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

How should I use the fbox command correctly to avoid producing a Bad Box message?How to put a long piece of text in a box?How to specify height and width of fboxIs there an arrayrulecolor-like command to change the rule color of fbox?What is the command to highlight bad boxes in pdf?Why does fbox sometimes place the box *over* the graphic image?how to put the text in the boxHow to create command for a box where text inside the box can automatically adjust?how can I make an fbox like command with certain color, shape and width of border?how to use fbox in align modeFbox increase the spacing between the box and it content (inner margin)how to change the box height of an equationWhat is the use of the hbox in a newcommand command?

Doxepinum Nexus interni Notae | Tabula navigationis3158DB01142WHOa682390"Structural Analysis of the Histamine H1 Receptor""Transdermal and Topical Drug Administration in the Treatment of Pain""Antidepressants as antipruritic agents: A review"

inputenc: Unicode character … not set up for use with LaTeX The Next CEO of Stack OverflowEntering Unicode characters in LaTeXHow to solve the `Package inputenc Error: Unicode char not set up for use with LaTeX` problem?solve “Unicode char is not set up for use with LaTeX” without special handling of every new interesting UTF-8 characterPackage inputenc Error: Unicode character ² (U+B2)(inputenc) not set up for use with LaTeX. acroI2C[I²C]package inputenc error unicode char (u + 190) not set up for use with latexPackage inputenc Error: Unicode char u8:′ not set up for use with LaTeX. 3′inputenc Error: Unicode char u8: not set up for use with LaTeX with G-BriefPackage Inputenc Error: Unicode char u8: not set up for use with LaTeXPackage inputenc Error: Unicode char ́ (U+301)(inputenc) not set up for use with LaTeX. includePackage inputenc Error: Unicode char ̂ (U+302)(inputenc) not set up for use with LaTeX. … $widehatleft (OA,AA' right )$Package inputenc Error: Unicode char â„¡ (U+2121)(inputenc) not set up for use with LaTeX. printbibliography[heading=bibintoc]Package inputenc Error: Unicode char − (U+2212)(inputenc) not set up for use with LaTeXPackage inputenc Error: Unicode character α (U+3B1) not set up for use with LaTeXPackage inputenc Error: Unicode characterError: ! Package inputenc Error: Unicode char ⊘ (U+2298)(inputenc) not set up for use with LaTeX