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
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
add a comment |
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
Related Question: Detecting if inside a tikzpicture
– Peter Grill
Nov 7 '18 at 23:30
add a comment |
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
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
environments conditionals
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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?
2
@Nickoley: Note that this doesn't work any longer wheninneris used inside another environment inside themyenvenvironment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then@currenvir` will becenterwheninneris 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 amyenvenvironment. However, both@currenvirand@myenvnamedisplay 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@currenvirto not belongas it used to be. Fixed by adding a*.
– egreg
6 mins ago
add a comment |
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.
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 isstrcmpin XeTeX andpdfstrcmpin pdfTeX. One can loadpdftexcmdsand usepdf@strcmpin all engines (including LuaTeX).
– egreg
May 20 '11 at 10:04
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
2
@Nickoley: Note that this doesn't work any longer wheninneris used inside another environment inside themyenvenvironment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then@currenvir` will becenterwheninneris 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 amyenvenvironment. However, both@currenvirand@myenvnamedisplay 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@currenvirto not belongas it used to be. Fixed by adding a*.
– egreg
6 mins ago
add a comment |
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?
2
@Nickoley: Note that this doesn't work any longer wheninneris used inside another environment inside themyenvenvironment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then@currenvir` will becenterwheninneris 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 amyenvenvironment. However, both@currenvirand@myenvnamedisplay 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@currenvirto not belongas it used to be. Fixed by adding a*.
– egreg
6 mins ago
add a comment |
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?
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?
edited 6 mins ago
answered May 20 '11 at 8:32
egregegreg
729k8819263235
729k8819263235
2
@Nickoley: Note that this doesn't work any longer wheninneris used inside another environment inside themyenvenvironment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then@currenvir` will becenterwheninneris 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 amyenvenvironment. However, both@currenvirand@myenvnamedisplay 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@currenvirto not belongas it used to be. Fixed by adding a*.
– egreg
6 mins ago
add a comment |
2
@Nickoley: Note that this doesn't work any longer wheninneris used inside another environment inside themyenvenvironment, e.g. beginmyenvbegincenterinnerendcenterendmyenv. Then@currenvir` will becenterwheninneris 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 amyenvenvironment. However, both@currenvirand@myenvnamedisplay 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@currenvirto not belongas 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
add a comment |
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.
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 isstrcmpin XeTeX andpdfstrcmpin pdfTeX. One can loadpdftexcmdsand usepdf@strcmpin all engines (including LuaTeX).
– egreg
May 20 '11 at 10:04
add a comment |
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.
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 isstrcmpin XeTeX andpdfstrcmpin pdfTeX. One can loadpdftexcmdsand usepdf@strcmpin all engines (including LuaTeX).
– egreg
May 20 '11 at 10:04
add a comment |
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.
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.
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 isstrcmpin XeTeX andpdfstrcmpin pdfTeX. One can loadpdftexcmdsand usepdf@strcmpin all engines (including LuaTeX).
– egreg
May 20 '11 at 10:04
add a comment |
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 isstrcmpin XeTeX andpdfstrcmpin pdfTeX. One can loadpdftexcmdsand usepdf@strcmpin 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Related Question: Detecting if inside a tikzpicture
– Peter Grill
Nov 7 '18 at 23:30