# How can I expand a word? A command without a backslash?

In my source code I would like Snap! to be automatically replaced with Snap\textit{!}

\newcommand{Snap!}{Snap\textit{!}}


Errors with "missing number, treating as zero"

• With standard latex you cannot, this is probably a better job for the search-and-replace in your editor. – daleif Apr 13 at 11:08
• What's the problem with \newcommand{\Snap}{Snap\textit{!}}? What is the reason you want to get rid of the backslash? – Henri Menke Apr 13 at 11:43
• @HenriMenke I wanted to write a "normal" paragraph in source, so the source was succinct and readable, and the output had the correct stylisation applied. So it wasn't preferred in the first place, but in the end I did resort to trying \Snap too, but that deletes spaces directly after itself. – theonlygusti Apr 13 at 11:45
• @theonlygusti More "readable" source is always to wrong reason. If you want that, use Markdown and convert to LaTeX. Then you can also do things like in your question using Pandoc Lua filters. – Henri Menke Apr 13 at 11:49
• \Snap\  puts the space back, there are several answers on this site about that. – David Carlisle Apr 13 at 17:53

I agree with a comment that this is best handled in an editor, rather than in LaTeX. But in the spirit of providing a LaTeX answer...

Pure listofitems version

Here, a macro \snap will examine its argument and do the replacement of Snap! with Snap\textit{!}. However, unlike the tokcycle approach shown below, this version will not find Snap! occurrences inside of arguments, for example, \textbf{blah Snap! blah}.

\documentclass{article}
\usepackage{tokcycle,listofitems,xcolor}
\setsepchar{Snap!}
\newcommand\snap[1]{%
\foreachitem\z\in\mysnaps[]{%
\ifnum\zcnt=1\else Snap\textit{!}\fi
\z
}%
}
\begin{document}
\snap{%
There is Snap! and other Snap  and snappy *Snap!* words!
}
\end{document}


tokcycle version

If you can live with ! being considered a "letter" (catcode-11), then there is this tokcycle environment \snap...\endsnap that will find exact instances of Snap! and replace the ! as italic.

\documentclass{article}
\usepackage{tokcycle,listofitems,xcolor}
\catcode!=11
\setsepchar{Snap!}
\newcommand\testdict{%
\if\relax\detokenize\expandafter{\currentword}\relax\else
{\ignoreemptyitems
\ifnum\listlen\dictcompA[]=0\relax
\else
\ifnum\listlen\dictcompB[]>1\relax
\else
\fi
\fi
\fi
\gdef\currentword{}%
}
\makeatletter
\xtokcycleenvironment\snap
{\testdict\groupedcytoks{\processtoks{##1}\testdict}}
{\stripgroupingtrue\def\currentword{}}
{}
\makeatother
\newcommand\autohighlightStyleA{\snapit}
\newcommand\autohighlightStyleB{\snapit}
\newcommand\snapit[1]{Snap\textit{!}}
\begin{document}
\snap

There is Snap! and other Snap  and snappy *Snap!* words!

\endsnap
\end{document}
`