Macro that reacts to the last symbol of the argument

If ! is not supposed to appear anywhere else in the argument to \mynode, it's quite easy:

\documentclass{article}
\usepackage{tikz}

\makeatletter
\newcommand\mynode[1]{\mynodeaux#1!\@nil}
\def\mynodeaux#1!#2\@nil{%
  \if\relax\detokenize{#2}\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\node at (0,0) {#1};}%
  {\node[fill=red] at (0,0) {#1};}%
}
\makeatother

\begin{document}

\begin{tikzpicture}
\mynode{text}
\end{tikzpicture}

\bigskip

\begin{tikzpicture}
\mynode{text!}
\end{tikzpicture}

\end{document}

enter image description here

However, I'd suggest a different strategy, more in line with standard LaTeX syntax.

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\NewDocumentCommand{\mynode}{sm}{%
  \IfBooleanTF{#1}
   {\node[fill=red] at (0,0) {#2};}%
   {\node at (0,0) {#2};}%
}

\begin{document}

\begin{tikzpicture}
\mynode{text}
\end{tikzpicture}

\bigskip

\begin{tikzpicture}
\mynode*{text}
\end{tikzpicture}

\end{document}

How about this:

\documentclass{article}
\usepackage{tikz}
\makeatletter
\def\mynode{\@ifnextchar[{\mynode@}{\mynode@[]}}%]
\def\mynode@[#1]#2{\mynode@@{#1}#2\empty!\empty\relax}
\def\mynode@@#1#2!\empty#3\relax{
    \def\temp{#3}
    \node[#1,/utils/exec=\ifx\temp\empty\else\pgfkeysalso{fill=red}\fi] at (0,0) {#2};
}


\makeatother


\begin{document}
\begin{tikzpicture}
\mynode{hi!}

\mynode[xshift=1cm]{the!re}
\end{tikzpicture}

\end{document} 

(I stole David Carlisle's code to check whether the argument ends in an exclamation point.)

enter image description here


enter image description here

\def\remshriek#1{\xremshriek#1\empty!\empty\relax}
\def\xremshriek#1!\empty#2\relax{%
\def\tmp{#2}
\ifx\tmp\empty\else[fill]\fi
#1}

\remshriek{text}

\remshriek{text! text}

\remshriek{text!}

Tags:

Strings