# Substitute all 'A' with \textsc{a}

Can I use catcode to substitute all capital letters in my document to \textsc{\myletter}?

EDIT: I would like to make this by a function, like:

\thisismystring{BLABLABLA}, that converts into BL{\textsc{a}}BL{\textsc{a}}BL{\textsc{a}}.

-
You'd have problems for all commands whose name contains a capital letter. And possibly other ones because capital letters enter sometimes in the inner syntax of TeX and in those cases they must have catcode 11 or 12. –  egreg Mar 24 '13 at 23:04
The last one is certainly possible, either pure TeX or with the xstring package and its \StrSubstitute macro. –  Qrrbrbirlbel Mar 24 '13 at 23:19

Contrary to my comment, the xstring package’s \StrSubstitute fails with

\usepackage{xstring}
\newcommand*{\replaceA}[1]{%
\StrSubstitute{#1}{A}{\textsc{a}}%
}


The following solution uses a TeX macro.

## Code

\documentclass{article}
\makeatletter
\newcommand*{\replace}[3]{%
\def\qrr@replace##1#2##2\@qrr@replace{##1\if\relax\detokenize{##2}\relax\else#3\qrr@replace##2\@qrr@replace\fi}%
\qrr@replace#1#2\@qrr@replace}
\newcommand*{\replaceA}[1]{\replace{#1}{A}{\textsc{a}}}
\makeatother
\begin{document}
\replaceA{alAbAmA}
\replaceA{BLABLABLA}
\replaceA{not one capital a}

\replace{ALABAMA}{ABA}{\textsc{aba}}
\end{document}


## Output

-
Exactly what I was looking for. I'll make one function for each letter. :) –  user13596 Mar 24 '13 at 23:46
@user13596 I have added a comprehensive solution that works similar to \StrSubstitute. –  Qrrbrbirlbel Mar 24 '13 at 23:49
Are you sure \StrSubstitute fails? –  egreg Mar 24 '13 at 23:54
@egreg Without the until-now-unknown-to-me \expandarg, it does for me. Or in other words: I noticed that \StrSubstitute expands its arguments (or does some other nasty things to it) and I abandoned the xstring after realizing that neither \protecting nor \noexpanding had worked. –  Qrrbrbirlbel Mar 25 '13 at 0:05
The documentation of xstring makes it clear that with the default setting, arguments are passed through \edef that's not very kind to \textsc. With \expandarg only one step of expansion is performed (which is necessary if you need to pass a macro to \replaceA). Otherwise \noexpandarg is even safer. –  egreg Mar 25 '13 at 0:37

With xstring it's easy:

\documentclass{article}
\usepackage{xstring}

\newcommand{\replaceA}[1]{%
\begingroup\expandarg
\StrSubstitute{#1}{A}{\textsc{a}}%
\endgroup
}

\newcommand{\mystring}{BLABLA}

\begin{document}

\replaceA{BLABLA}

\replaceA{\mystring}

\end{document}


An implementation with the l3regex module of LaTeX3

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\replaceA}{smo}
{
\IfNoValueTF{#3}
{
\IfBooleanTF{#1}
{ \repla_main:NV \l_tmpa_tl #2 }
{ \repla_main:Nn \l_tmpa_tl { #2 } }
\tl_use:N \l_tmpa_tl
}
{
\IfBooleanTF{#1}
{ \repla_main:NV #3 #2 }
{ \repla_main:Nn #3 { #2 } }
}
}
\cs_new_protected:Npn \repla_main:Nn #1 #2
{
\tl_set:Nn #1 { #2 }
\regex_replace_all:nnN { A } { \c{textsc}\cB\{a\cE\} } #1
}
\cs_generate_variant:Nn \repla_main:Nn { NV }
\ExplSyntaxOff

\newcommand{\mystring}{BLABLA}

\begin{document}

\replaceA{BLABLA}

\replaceA*{\mystring}

\replaceA{BLABLA}[\newstring]

\show\newstring

\replaceA*{\mystring}[\newstring]

\show\newstring

\end{document}


If no optional argument is specified, the string obtained after replacement is immediately used, otherwise it's stored in the specified control sequence.

The *-variant allows for a macro to be passed as argument.

-