1

I want to form a macro which will enable me to get the following result.

Let's say I want L, M & N to be active in my document.

And I also want character p to be functional in the code such that

  • If L is followed by p it changes to q.
  • If M is followed by p it changes to r.
  • If N is followed by p it changes to s.

Can I define character p in this way?


The code provided by @Red-Cloud is great, but I have a different setup which I initially excluded from the MWE as not necessary, but now I find it necessary to be a part of it. This is the actual setup where I want to use this code.

There is one character 'x' which sets up a condition for the active characters. That condition says if an active character (L, M & N in our case) is not followed by 'x' then add X after them.

Code -

\documentclass{article}
\makeatletter
\catcode`L=\active
\protected\def L{\bgroup a\futurelet\tmp\check}
\catcode`M=\active
\protected\def M{\bgroup b\futurelet\tmp\check}
\catcode`N=\active
\protected\def N{\bgroup c\futurelet\tmp\check}
\def\check{\ifx\tmp x\egroup\expandafter\@gobble\else X\egroup\fi}
\makeatother

\begin{document}
    LMN
\end{document}

This generates -

1

Without changing this setup I want to add the previously asked settings (initial question) to it. In a way if I have LpMpNp in my input I want qXrXsX (look at condition set 1 and 2) Is it possible?

  • Shouldn't you stop asking again and again the same question? At least give a reference to the other similar ones. – egreg Jul 12 at 14:25
  • @egreg I've not yet received any complete solution, I'm trying to reframe the question in a simpler way. I was asked to explain my question 'better'. I accepted my fault there and then tried to reframe it here. I purposely did not mention the references, because it might be more confusing for the reader. I'm sorry if I broke any rule of the site. – Niranjan Jul 12 at 15:28
3
\documentclass{article}    
\begin{document}    
\begingroup
\catcode`L=\active
\catcode`M=\active
\catcode`N=\active
\makeatletter
\def L{\@ifnextchar p{q\@gobble}{}}
\def M{\@ifnextchar p{r\@gobble}{}}
\def N{\@ifnextchar p{s\@gobble}{}}
\makeatother
Lp Lq Lr

Mp Mq Mr

Np Nq Nr    
\endgroup

Lp Lq Lr

Mp Mq Mr

Np Nq Nr    

\end{document}

enter image description here

  • Caveat: this will gobble spaces directly following the main letter. – egreg Jul 12 at 9:14
  • Thanks, This is working perfectly fine, but I've few more conditional settings which I excluded in my MWE. Unfortunately I don't know how to use the commands that you've recommended with my setup. Please check the edit. – Niranjan Jul 12 at 13:11
  • \def L{\@ifnextchar p{qX\@gobble}{}} – user187802 Jul 12 at 17:28
  • This is probably a good hack, but what I want to achieve is having 'X' with the specific command \def\check{\ifx\tmp x\egroup\expandafter\@gobble\else X\egroup\fi} and replacements (LMN > qrs) by a different set of commands which you have provided. I don't want to code qX, rX and sX. Can you please suggest a command which will enable me to combine both of them? So that one set of commands will change L to q and the other command will see that x is absent and hence add 'X'. – Niranjan Jul 13 at 6: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.