Applying an operation only to capitals
\documentclass[a4paper]{article}
\makeatletter
\DeclareRobustCommand{\emphcap}[1]{\begingroup\emph@cap#1\@nil\endgroup}
\def\emph@cap#1{%
\ifx#1\@nil
\expandafter\@gobble
\else
\emph@@cap{#1}%
\fi
\emph@cap}
\def\emph@@cap#1{%
\ifnum\uccode`#1=`#1\relax
\itshape#1\emph@captrue
\else
\ifemph@cap\/\else\fi\upshape#1\emph@capfalse
\fi}
\newif\ifemph@cap
\makeatother
\begin{document}
\emphcap{THiSiSWHaTIWaNT}
\end{document}
This assumes that your input contains only normal letters (no accents). The result is really ugly.
An approach using LaTeX3: the important command is \regex_replace_all:nnN
. Its first argument is a regular expression (here, [A-Z]
matches any uppercase letter); its second argument the replacement, here \textit
followed by \0
(what the regular expression matched); and the third is a token list variable on which we want to do the replacement.
\documentclass[a4paper]{article}
\usepackage{l3regex}
\ExplSyntaxOn
\cs_new_protected:Npn \emphcap #1
{
\tl_set:Nn \l_tmpa_tl {#1}
\regex_replace_all:nnN { [A-Z] } { \c{textit} \0 } \l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\begin{document}
\emphcap{THiS iS \texttt{WHaT} I WaNT}
\end{document}
Of course, spaces are preserved, as are any formatting commands within the argument (here I put \texttt
for the demo). This should work with any recent enough version of the l3kernel
and l3experimental
bundles (February 2012, say).
Alternatively, you could use the replacement
\regex_replace_all:nnN { [A-Z]+ } { \c{textit} \cB\{ \0 \cE\} } \l_tmpa_tl
I changed the regex to match any number of consecutive uppercase letters, and I changed the replacement text to add braces around the argument to \textit
. This avoids the extra space inserted in \textit{N}\textit{T}
. The output:
Ok, here is an other approach dedicated to LuaLaTeX fans (and future fans of LuaLaTeX). I think it is a good example to show how easy it is to write a few (easy to understand) lines of Lua code. In the provided Lua code one can check the input string for every char and format any LaTeX string you need without cryptic TeX commands.
It is good practice to write the lua functions in a separate file with the extension .lua. For this MWE I use the filecontents
environment instead to provide an extra file for the lua script.
\documentclass{book}
\usepackage{filecontents}
\begin{filecontents*}{luaFunctions.lua}
function emphcaps(input)
outputString = ""
len = string.len(input)
for i = 1, len, 1 do --for each char in string
ascii = string.byte(input, i) -- convert the char in a decimal
if(ascii >= 65 and ascii <= 90) then -- upper case (look to the ASCII table)
outputString = outputString.."\\emph{"..string.char(ascii).."}"
else
outputString = outputString..string.char(ascii)
end
end
tex.print(outputString)
end
\end{filecontents*}
% read the external lua file to declare the defined function,
% but without execute the Lua function
\directlua{dofile("luaFunctions.lua")}
% latex command to execute the lua function
\def\emphcaps#1{\directlua{emphcaps("#1")}}
\begin{document}
\noindent
\emphcaps{YaWeH}\\
\emphcaps{LuaLaTeX}
\end{document}
Edit: After the great comment from Khaled Hosny I want to provide a very straight solution. It shows again the power of Lua ;-). The new function replaces all (thats what the +
after %u
stands for) upper chars (thats what the %u
stands for) with \emph{}
. The %1
holds the value which is surrounded by the ( )
in the search pattern.
function emphcaps(input)
output, count = string.gsub(input, '(%u+)', '\\emph{%1}')
tex.print(output)
end