2

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"

5
  • 3
    With standard latex you cannot, this is probably a better job for the search-and-replace in your editor.
    – daleif
    Apr 13, 2021 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? Apr 13, 2021 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. Apr 13, 2021 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. Apr 13, 2021 at 11:49
  • 1
    \Snap\ puts the space back, there are several answers on this site about that. Apr 13, 2021 at 17:53

1 Answer 1

3

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]{%
  \readlist\mysnaps{#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}

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.

\documentclass{article}
\usepackage{tokcycle,listofitems,xcolor}
\catcode`!=11
\setsepchar{Snap!}
\newcommand\testdict{%
  \if\relax\detokenize\expandafter{\currentword}\relax\else
    {\ignoreemptyitems
      \greadlist\dictcompA{\currentword}}%
    \readlist\dictcompB{\currentword}%
    \ifnum\listlen\dictcompA[]=0\relax
      \addcytoks[1]{\autohighlightStyleA}%
      \addcytoks[1]{\expandafter{\currentword}}
    \else
      \ifnum\listlen\dictcompB[]>1\relax
        \addcytoks[1]{\autohighlightStyleB}%
        \addcytoks[1]{\expandafter{\currentword}}
      \else
        \addcytoks[1]{\currentword}%
      \fi
    \fi
  \fi
  \gdef\currentword{}%
}
\makeatletter
\xtokcycleenvironment\snap
{\tctestifcatnx A##1{\g@addto@macro\currentword{##1}}
  {\testdict\addcytoks{##1}}}
{\testdict\groupedcytoks{\processtoks{##1}\testdict}}
{\g@addto@macro\currentword{##1}}
{\testdict\addcytoks{##1}}
{\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .