I hope I can have a command MyFormatter, which can make letters p and k bold and make letters m and t italic.
For example:
\MyFormatter{Supermarket}
will output as follows:
Supermarket
How to write the command \MyFormatter in LaTeX?
I hope I can have a command MyFormatter, which can make letters p and k bold and make letters m and t italic.
For example:
\MyFormatter{Supermarket}
will output as follows:
Supermarket
How to write the command \MyFormatter in LaTeX?
Here's a straightforward solution using expl3. If you need it to work in subgroups \MyFormatter{Supermarket and \MakeUppercase{Supermarket}} you can check this solution.
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \myformatter { +m } { \xmllmx_myformatter:n { #1 } }
\cs_new_protected:Npn \xmllmx_myformatter:n #1
{
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_replace_all:Nnn \l_tmpa_tl { p } { \textbf{p} }
\tl_replace_all:Nnn \l_tmpa_tl { k } { \textbf{k} }
\tl_replace_all:Nnn \l_tmpa_tl { m } { \textit{m} }
\tl_replace_all:Nnn \l_tmpa_tl { t } { \textit{t} }
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
It's easy and flexible with regular expressions:
\documentclass{article}
\usepackage{xparse,l3regex}
\ExplSyntaxOn
\NewDocumentCommand{\myformatter}{m}
{
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_replace_all:nnN
{ ([kp]+) } { \c{textbf}\cB\{ \1 \cE\} }
\l_tmpa_tl
\regex_replace_all:nnN
{ ([mt]+) } { \c{textit}\cB\{ \1 \cE\} }
\l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\begin{document}
\myformatter{Supermarket}
\end{document}
An approach using catcodes that works if the \MyFormatter appears in the clear and is not part of an argument. As egreg pointed out in this regard, \mbox{\MyFormatter{Supermarket}} fails.
However, I jokingly retort in my comment that the way to get around the fail-case is to put the \mbox inside the MyFormatter, not the other way around. Of course, the new drawback with this "fix" is that the "m" of \mbox is now active, and so an additional \let is required: \let\MBOX\mbox\MyFormatter{\MBOX{Supermarket}}.
Finally, wipet comments that one could argue that the problem lies in the definition of \mbox, in that \hbox{\MyFormatter{Supermarket}} does not suffer the same problem.
\documentclass{article}
\let\BB\textbf
\let\II\textit
\let\CC\catcode
\let\AC\active
\let\SVP p \let\SVK k \let\SVM m \let\SVT t
\def\CODESON{\CC`p=\AC \CC`k=\AC \CC`m=\AC \CC`t=\AC }
\def\CODESOFF{\CC`p=11 \CC`k=11 \CC`m=11 \CC`t=11 }
\CODESON
\def p{\BB{\SVP}}
\def k{\BB{\SVK}}
\def m{\II{\SVM}}
\def t{\II{\SVT}}
\CODESOFF
\newcommand\MF[1]{#1\CODESOFF}
\newcommand\MyFormatter{\CODESON\MF}
\begin{document}
Supermarket \MyFormatter{Supermarket} Supermarket
\mbox{\MyFormatter{Supermarket}} fails, but
\let\MBOX\mbox\MyFormatter{\MBOX{Supermarket}} works
\end{document}
\mbox{\MyFormatter{Supermarket}}– egreg Jun 30 '16 at 11:00\MyFormatter{Supermarket}doesn't hyphenate, why would one need to put it into an\mbox?;^)– Steven B. Segletes Jun 30 '16 at 11:14\let\MB\mbox\MyFormatter{\MB{Supermarket}}. Isn't that obvious??8^b– Steven B. Segletes Jun 30 '16 at 11:16\hbox{\MyFormater{Supermarket}}works. This is LaTeX problem that\mboxis defined as macro with parameter. It needs not. – wipet Jun 30 '16 at 11:20\hboxand\mbox(with parameter or not, that's a different topic). LaTeX users aren't and advertising\hboxis not the best service to them. Besides,\mboxwas just an example; any command with argument would suffer of the same problem, including plain TeX ones. – egreg Jun 30 '16 at 12:04\hboxis best service to them because they come to know that there are other approaches than only LaTeX. :-). Or they can define\def\mbox{\leavevmode\hbox}– wipet Jun 30 '16 at 12:09