Condition “if within another command” in LaTeX

Condition “if within another command” in LaTeX



I would like to define a LaTeX (!) command which would change its behavior if it occurs withing the scope of another command. Something like:


newcommandaaa[1]#1
newcommandbbb[1] IF WITHIN aaa >>> it #1
ELSE> bf #1



Sorry for the "code" but I have little idea where to start looking for it or how to formulate my question correctly.



For example,


bbbsometext



prints: sometext



while


aaabbbsometext



prints: sometext



What I was able to find is only a similar question from some years ago: Detecting if inside section. The answer was to use "modes" in ConTeXt which is impossible for me as I am restricted to LaTeX due to very specific packages I need.





Does it have to be bbb detecting if it's within aaa? Could aaa locally redefine bbb instead?
– Teepeemm
Aug 30 at 1:42


bbb


aaa


aaa


bbb





Thank you for your comment. The latter is also an option of course but I still do not know how it could be implemented.
– Wa Wo
Aug 30 at 1:46





This question has already got its answer, I’m wondering what is the typical use of this? Any example?
– Mithun
Aug 30 at 16:12




4 Answers
4



EDITED to reflect helpful insight from Jonas that the original method (setting 0/1) could be spoofed with repeated invocations of bbb inside of aaa. Thus I converted over to an analogous counter approach, such that the counter value reflects the depth of nesting.


bbb


aaa


documentclassarticle

newcounterinaaa
newcommandaaa[1]stepcounterinaaa#1addtocounterinaaa-1
newcommandbbb[1]%
ifnumvalueinaaa=0textbf#1elserelaxtextit#1fi
begindocument
bbbsometext

aaabbbsometext text

aaaaaabbbfoobbbbar
enddocument



enter image description here





Thank you very much. It worked for me! (it was of course not about bold or italics)
– Wa Wo
Aug 30 at 1:58





This solution may fail if aaa is used within itself, see this answer.
– Jonas Granholm
Aug 30 at 21:58


aaa





aaaaaabbbfoobbbbar gives foo bar instead of foo bar. The problem is that the inner aaa “turns off” inaaa when it ends, before the second bbb is evaluated.
– Jonas Granholm
Aug 31 at 6:51


aaaaaabbbfoobbbbar


aaa


inaaa


bbb





A trick that should work is to increase and decrease inaaa instead of setting it to 1 and 0, so that it measures nesting level instead.
– Jonas Granholm
Aug 31 at 6:53


inaaa





@JonasGranholm Thanks. I will adopt a counter approach.
– Steven B. Segletes
Aug 31 at 12:42



You can use the newif command to define a new (very low-level) conditional and commands that alter the conditional test:


newif


documentclassarticle

newififinsideaaa

newcommandaaa[1]%
insideaaatrue
#1%
insideaaafalse

newcommandbbb[1]
ifinsideaaa
itshape #1%
else
bfseries #1%
fi


begindocument

bbbSome text outside of textttstringaaa

aaabbbSome text inside of textttstringaaa

enddocument



enter image description here



The new commands defined by newifif<foo> are:


newifif<foo>


if<foo>


if<foo> ... else ... fi


<foo>true


<foo>false



As a side note, you should use itshape and bfseries instead of it and bf in LaTeX. The latter are outdated.


itshape


bfseries


it


bf





Thank you very much for your explanation! it and bf are just examples. It's actually about other command which would just distract if mentioned.
– Wa Wo
Aug 30 at 2:00






This solution may fail if aaa is used within itself, see this answer.
– Jonas Granholm
Aug 30 at 21:58


aaa



I tend to avoid the TeX primitives if.. whenever avoiding them is possible.


if..


documentclassarticle

makeatletter
newcommandMy@CheckWhetherinAAA
newcommandMy@SetInAAATrueletMy@CheckWhetherinAAA=@firstoftwo
newcommandMy@SetInAAAFalseletMy@CheckWhetherinAAA=@secondoftwo
globalMy@SetInAAAFalse

newcommandaaa[1]%
My@CheckWhetherinAAA#1%
My@SetInAAATrue#1My@SetInAAAFalse%
%

newcommandbbb[1]%
My@CheckWhetherinAAAtextit%
textbf%
#1%

%
% The result of the following is the same but LaTeX has to shuffle
% around more tokens:
%
% newcommandbbb[1]%
% My@CheckWhetherinAAAtextit#1%
% textbf#1%
% %
%
makeatother


begindocument
bbbsome text

aaabbbsome text

aaaaaabbbsome text aaabbbsome more text

begingroup aaaaaabbbsome text endgroupaaabbbsome more text

bbbsome text

enddocument



With this approach, the tokens forming the argument of aaa get doubled and gobbled.


aaa



This can be avoided by defining another helper-macro:


documentclassarticle

makeatletter
newcommandMy@CheckWhetherinAAA
newcommandMy@SetInAAATrueletMy@CheckWhetherinAAA=@firstoftwo
newcommandMy@SetInAAAFalseletMy@CheckWhetherinAAA=@secondoftwo
newcommandMy@SetAAAconditions[1]My@SetInAAATrue#1My@SetInAAAFalse%
globalMy@SetInAAAFalse

newcommandaaa[1]%
My@CheckWhetherinAAA@firstofone%
My@SetAAAconditions%
#1%
%

newcommandbbb[1]%
My@CheckWhetherinAAAtextit%
textbf%
#1%

%
% The result of the following is the same but LaTeX has to shuffle
% around more tokens:
%
% newcommandbbb[1]%
% My@CheckWhetherinAAAtextit#1%
% textbf#1%
% %
%
makeatother


begindocument
bbbsome text

aaabbbsome text

aaaaaabbbsome text aaabbbsome more text

begingroup aaaaaabbbsome text endgroupaaabbbsome more text

bbbsome text

enddocument





global, makeatletter, makeatother, @firstoftwo, and @secondoftwo seems like a lot of machinery to avoid a tex primitive. And should My@SetInaFalse be My@SetInAAAFalse?
– Teepeemm
Aug 30 at 22:09


global


makeatletter


makeatother


@firstoftwo


@secondoftwo


My@SetInaFalse


My@SetInAAAFalse





@JonasGranholm Thanks for editing.
– Ulrich Diez
Aug 31 at 0:09





@Teepeemm Indeed, should be My@SetInAAAFalse. I avoid things like if...#1...else...fi because providing unbalanced if/else/fias components of arguments might cause problems... As a rule of thumb macros get a little more "user-proof" by avoiding putting an argument (where you don't know what tokens the user will provide) somewhere between if..else..fi. When I use them, I often do if..expandafter@firstoftwoelseexpandafter@secondoftwofi<true-case-stuff with macro arguments>false-case-stuff with macro arguments>
– Ulrich Diez
Aug 31 at 0:21


My@SetInAAAFalse


if...#1...else...fi


if


else


fi


if..else..fi


if..expandafter@firstoftwoelseexpandafter@secondoftwofi<true-case-stuff with macro arguments>false-case-stuff with macro arguments>



Siracusa's answer may fail if aaa is used within itself, as in the following example:


aaa


aaa
aaabbbfoo
bbbbar



This will give foo bar instead of foo bar.



Ulrich's and the accepted answer do not have this weakness and are good solutions, especially if you want to define both versions of bbb at the same time.
A quicker and simpler way that also behaves as expected in the example above is to let aaa redefine bbb locally, as Teepeemm commented:


bbb


aaa


bbb


newcommandbbb[1]textbf#1
newcommandaaa[1]bgroupdefbbb##1textit##1#1egroup



The double ## in the second line is needed because it is a definition inside another definition.


##



(This method is by the way also useful if bbb should only be defined inside aaa – just remove the first line.)


bbb


aaa





It looks like you can also take Ulrich's of delaying #1 and say newcommandbbbtextbf. It looks like that lets you avoid ##1 entirely.
– Teepeemm
Aug 30 at 22:06


#1


newcommandbbbtextbf


##1





Indeed. I included the full form since OP mentioned replacing the command with something more complicated. Another way to avoid ##1 is to add newcommandbbbinaaa[1]textit#1 and use newcommandaaa[1]bgroupletbbbbbbinaaa#1egroup instead.
– Jonas Granholm
Aug 30 at 22:29



##1


newcommandbbbinaaa[1]textit#1


newcommandaaa[1]bgroupletbbbbbbinaaa#1egroup





@JonasGranholm Defining both versions of bbb at the same time is recommendable only in case there are more macros wherein bbb shall work in the same way in which it shall work within aaa.
– Ulrich Diez
Aug 31 at 0:43


bbb


bbb


aaa



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)