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

Stereotypical names

Female=gender counterpart?

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

In Star Trek IV, why did the Bounty go back to a time when whales were already rare?

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

Can a Bard use an arcane focus?

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?

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

Is there enough fresh water in the world to eradicate the drinking water crisis?

Can I rely on these GitHub repository files?

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

Latex for-and in equation

How to deal with or prevent idle in the test team?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Adding empty element to declared container without declaring type of element

How to interpret the phrase "t’en a fait voir à toi"?

Issues with Installing Powershell to Kali Machine

How to deal with loss of decision making power over a change?

How can I raise concerns with a new DM about XP splitting?

What should I use for Mishna study?

Proof of Lemma: Every integer can be written as a product of primes

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?



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:



documentclass{article}

newenvironment{myenv}[0]{at start}{ at end}

newcommand{inner}[0]{%
% if iside a myenv
(inner)
% else
begin{myenv}
(inner)%
end{myenv}
% end
}

begin{document}

begin{myenv}
inner
end{myenv}

inner

end{document}


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:



documentclass{article}

newenvironment{myenv}[0]{at start}{ at end}

newcommand{inner}[0]{%
% if iside a myenv
(inner)
% else
begin{myenv}
(inner)%
end{myenv}
% end
}

begin{document}

begin{myenv}
inner
end{myenv}

inner

end{document}


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:



documentclass{article}

newenvironment{myenv}[0]{at start}{ at end}

newcommand{inner}[0]{%
% if iside a myenv
(inner)
% else
begin{myenv}
(inner)%
end{myenv}
% end
}

begin{document}

begin{myenv}
inner
end{myenv}

inner

end{document}


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:



documentclass{article}

newenvironment{myenv}[0]{at start}{ at end}

newcommand{inner}[0]{%
% if iside a myenv
(inner)
% else
begin{myenv}
(inner)%
end{myenv}
% end
}

begin{document}

begin{myenv}
inner
end{myenv}

inner

end{document}


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
newcommand{inner}{%
ifx@currenvir@myenvname
(inner)
else
begin{myenv}(inner)end{myenv}
fi}
newcommand*@myenvname{myenv}
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. begin{myenv}begin{center}innerend{center}end{myenv}. 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
    13 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
    4 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
newcommand{inner}{%
ifx@currenvir@myenvname
(inner)
else
begin{myenv}(inner)end{myenv}
fi}
newcommand*@myenvname{myenv}
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. begin{myenv}begin{center}innerend{center}end{myenv}. 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
    13 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
    4 mins ago
















20














LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommand{inner}{%
ifx@currenvir@myenvname
(inner)
else
begin{myenv}(inner)end{myenv}
fi}
newcommand*@myenvname{myenv}
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. begin{myenv}begin{center}innerend{center}end{myenv}. 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
    13 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
    4 mins ago














20












20








20







LaTeX keeps the current environment in the macro @currenvir



makeatletter
newcommand{inner}{%
ifx@currenvir@myenvname
(inner)
else
begin{myenv}(inner)end{myenv}
fi}
newcommand*@myenvname{myenv}
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
newcommand{inner}{%
ifx@currenvir@myenvname
(inner)
else
begin{myenv}(inner)end{myenv}
fi}
newcommand*@myenvname{myenv}
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 4 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. begin{myenv}begin{center}innerend{center}end{myenv}. 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
    13 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
    4 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. begin{myenv}begin{center}innerend{center}end{myenv}. 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
    13 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
    4 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. begin{myenv}begin{center}innerend{center}end{myenv}. 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. begin{myenv}begin{center}innerend{center}end{myenv}. 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
13 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
13 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
4 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
4 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

Installing LyX: “No textclass is found.”LyX installation error- text class not found- 'Reconfigure' or...

(1602) Indiana Índice Designación y nombre Características orbitales Véase...

Universidad Autónoma de Occidente Índice Historia Campus Facultades Programas Académicos Medios de...