add tag
निरंजन
I am trying to locally make `//` active. With some help from [this](https://tex.stackexchange.com/a/96432) answer, I wrote this code:


```
\documentclass{article}
\usepackage{xparse}

\begin{document}
\makeatletter
\begingroup
\let\myslash/%
\catcode`\/\active
\NewDocumentCommand{/}{  }{%
  \@ifnextchar/{%
    \egroup
    \@gobble
  }{%
    {\myslash}%
  }%
}%

\bgroup \itshape abcd/a/ b

efgh//

abcd

foo
\endgroup
\makeatother
\end{document}
```

The output is almost satisfactory, but at the moment it cannot handle spaces. Like other TeX macros it is gobbling the space coming after a `/`. Hence I am getting `a/b` with `a/ b`. I was assuming that the `\myslash` macro which I have used in the false branch of the `\@ifnextchar`-conditional gobbles the space, but enclosing it in a group also isn't working. So it must be something else. What is it? How to make this code also look at spaces and add spaces when required?

As active characters are generally considered dangerous; is there any better/safe way to achieve what I want?
Top Answer
Skillmon
The following implements a `\niranjan@ifnextchar` that behaves similar to `\@ifnextchar`, though there are a few differences:

- all assignments are grouped (standard LaTeX doesn't wrap things in `\begingroup ...\endgroup` here)
- spaces aren't ignored
- in the second macro `\niranjan@ifnch` this doesn't use `\reserved@c`, instead it only uses `\let\reserved@b\reserved@a` in the true case

with this your active `/` should work as you intended.

```
\documentclass{article}
\usepackage{xparse}

\makeatletter
\protected\long\def\niranjan@ifnextchar#1#2#3%
  {%
    \begingroup
      \let\reserved@d=#1%
      \def\reserved@a{#2}%
      \def\reserved@b{#3}%
      \futurelet\@let@token\niranjan@ifnch
  }
\protected\def\niranjan@ifnch
  {%
    \ifx\reserved@d\@let@token
      \let\reserved@b\reserved@a
    \fi
    \expandafter\endgroup
    \reserved@b
  }
\makeatother

\begin{document}
\makeatletter
\begingroup
\let\myslash/%
\catcode`\/\active
\NewDocumentCommand{/}{  }{%
  \niranjan@ifnextchar/{%
    \egroup
    \@gobble
  }{%
    \myslash
  }%
}%

\bgroup \itshape abcd/a/ b

efgh//

abcd

foo
\endgroup
\makeatother
\end{document}
```

This is assumed to be of educational nature, I'd simply use `expl3` functions here:

```
\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\cs_new:Npn \niranjan_slash:
  {
    \peek_meaning_remove:NTF \niranjan_slash:
      { \group_end: }
      { \__niranjan_saved_slash: }
  }
\NewDocumentCommand \SpecialSlash {}
  {
    \group_begin:
      \cs_set_eq:NN \__niranjan_saved_slash: /
      \char_set_active_eq:NN / \niranjan_slash:
      \char_set_catcode_active:N /
      \itshape
  }
\ExplSyntaxOff


\begin{document}
\SpecialSlash abcd/a/ b

efgh//

abcd

foo
\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.