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


Errors with "missing number, treating as zero"

  • 3
    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
  • 2
    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
  • 1
    \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}.

    \ifnum\zcnt=1\else Snap\textit{!}\fi
There is Snap! and other Snap  and snappy *Snap!* words!

enter image description here

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.

{\tctestifcatnx A##1{\g@addto@macro\currentword{##1}}

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


enter image description here

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.