Discussion:
conditional newcommand
(too old to reply)
François Patte
2025-01-12 11:23:52 UTC
Permalink
Bonjour,

I would like to define a newcommand with 2 different definitions,
depending if it is used inside or outside a specific environment.

Is it possible to define a test which returns 0 if the command is used
outside the environment and 1 if it is inside the environment?

Thank you.

F.P.
Ulrike Fischer
2025-01-12 11:40:56 UTC
Permalink
Post by François Patte
Bonjour,
I would like to define a newcommand with 2 different definitions,
depending if it is used inside or outside a specific environment.
Is it possible to define a test which returns 0 if the command is used
outside the environment and 1 if it is inside the environment?
sure. lots of commands change their behaviour depending on where
they are. E.g. all language commands change inside a otherlanguage
environment.

How to do it depends on the details, and also what you mean by
"inside", but e.g.

\documentclass{article}
\usepackage{etoolbox}
\newbool{myspecialenv}
\newcommand\testcommand{\ifbool{myspecialenv}{1}{0}}
\AddToHook{env/minipage/begin}{\booltrue{myspecialenv}}
\begin{document}
\testcommand


\begin{minipage}{3cm}
\testcommand
\end{minipage}
\end{document}
--
Ulrike Fischer
http://www.troubleshooting-tex.de/
François Patte
2025-01-12 17:38:53 UTC
Permalink
Post by Ulrike Fischer
Post by François Patte
Bonjour,
I would like to define a newcommand with 2 different definitions,
depending if it is used inside or outside a specific environment.
Is it possible to define a test which returns 0 if the command is used
outside the environment and 1 if it is inside the environment?
sure. lots of commands change their behaviour depending on where
they are. E.g. all language commands change inside a otherlanguage
environment.
How to do it depends on the details, and also what you mean by
"inside", but e.g.
\documentclass{article}
\usepackage{etoolbox}
\newbool{myspecialenv}
\newcommand\testcommand{\ifbool{myspecialenv}{1}{0}}
\AddToHook{env/minipage/begin}{\booltrue{myspecialenv}}
Thank you, I could define my command as I wished:
\providecommand{\croche}{%
\ifthenelse{\isodd{\testcommand}}{%
{\textup{\symbol{"0966}}}%
}%
{\textup{\bfseries o}}%
}%

In fact I have more than one command to define and I failed to find out
how to use only one ifthenelse test to do so.... I am not very skillful
in programmation, just curious if this is possible!

Thank you anyway.

F.P.

Loading...