1

Description

Hi, I was trying to make a simple \ifx statement to decide whether something was inside and environment and execute commands accordingly.

I want the notes in my document to be raggedright and in sans-serif. But, for a more consistent look, I also want them to be indented if outside an environment (mainly for descriptions, itemizes and enumerates) but to have no indentation if set inside one.

The code

So I tried the following simple code using \ifx and the @currenvir macro.

% Saving the \parindent value for later use
\newlength{\savedparindent}
\setlength{\savedparindent}{\parindent}
% Assigning the value 'document' to a control sequence for comparing later.
% I did this after noticing that outside any environment the \@currenvir macro would
% print the value 'document'
\newcommand*{\docname}{document}
% The actual \ifx conditional. The \Nota command (for note in spanish) is meant
% to be used in the document to add notes formatted in a distinctive way.
\newcommand{\Nota}[1]{
\begingroup
\makeatletter
\ifx\@currenvir\docname
    \raggedright\setlength{\parindent}{\savedparindent}\sffamily
    \else
    \raggedright\sffamily
   \fi  
\makeatother
#1
\endgroup
\par
}

The problem

In all my testing, the else part gets executed inside and environment and also outside, in 'normal' running text. Any ideas as to why it's not working? (Sorry if the solution is obvious, I have no experience in either programming or TeX witchcraft).

Don't know if this is relevant, but that code was tested compiling with luatex.

The solution

The \docname had to be defined after \makeatletter, also \par needs to be inside the group to work correctly.

1 Answer 1

2

You need to use \newcommand* (or \def) to define \docname so that it is not \long. A \long macro is never \ifx equal to a non-long one, even if they have the same replacement text.

Also

\makeatletter

has to be before the definition, having it inside any command is like having \verb inside a command it does not work as the input has already been tokenized.

and finally you need \par before the \endgroup as {\raggedright zzz} has no effect unless there is a paragraph break before the }

3
  • Hi, thanks for the quick response, had no idea about \long. After adding the fix you suggested the command still behaves erroneously: it sets the text without indentation (that is, the else statement) regardless of the environment. I'll edit the question to include the fix, and work on making a MWE that will also be added to the question.
    – JLanzuisi
    Apr 29, 2020 at 16:19
  • @JLanzuisi oh just spotted another error Apr 29, 2020 at 16:21
  • Wonderful, now it all works as intended, thanks
    – JLanzuisi
    Apr 29, 2020 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.