# Turning font-changing macros into font-changing “switches”

I want to turn a font-changing macro into a "switch" (I found the term in this answer), making macros like \macro{Text to be changed} work within the brakets, like {\switch text to be changed}, similarly to \textbf's and \bfseries's behavior.

I'm working with a thesis class which provides formatting commands like \titleformat, \authornameformat, etc., which can be changed. But in the whole class, those formatting commands are used like switches, so you have a \titlepage command that prints {\titleformat \titledata}, etc. I want to change some of these formatting commands into real small caps, which I defined as \DeclareRobustCommand{\smallcaps}[1]{\textls[10]{\scshape\MakeLowercase{#1}}}, but I don't know how I could make a switch to lowercase letters (i tried it with textcase package's \MakeTextLowercase too).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[osf]{libertine}
\usepackage{letterspace}

\DeclareRobustCommand{\smallcaps}[1]{\textls[10]{\scshape\MakeLowercase{#1}}}

\begin{document}

\smallcaps{These are real smallcaps}, {\smallcaps these are not} and this is normal.

\end{document}

-
In general the switch exists before the command with argument. E.g. \textbf is defined with the help of \bfseries: In latex.ltx you will find \DeclareTextFontCommand{\textbf}{\bfseries}. So you will have to be more specific about how your macro is defined if you want to go the other way round. –  Ulrike Fischer Feb 10 '12 at 17:06
Please, make a real example of usage. There's no gain in preferring the syntax {\smallcaps xyz} to \smallcaps{xyz}, in general. –  egreg Feb 10 '12 at 18:30

Let's define \textsfsl to choose \sffamily and \slshape:

\DeclareTextFontCommand{\textsfsl}{\sffamily\slshape}


The command's name need not begin with text. LaTeX will take care automatically of the italic correction for \textsfsl{text}, as can be seen in the following example:

\documentclass{article}
\DeclareTextFontCommand{\textsfsl}{\sffamily\slshape}
\begin{document}
abc \textsfsl{def}ghi

abc \sffamily\slshape def\/\normalfont ghi

abc \sffamily\slshape def\normalfont ghi
\end{document}


However, if what you need is the converse for processing section titles, there is another way: the package titlesec provides the \titleformat command, whose last mandatory argument can end with a command that takes as argument the section title: so, for example,

\documentclass{article}
\usepackage[osf]{mathpazo}
\usepackage{titlesec,microtype}
\DeclareRobustCommand{\smallcaps}[1]{\textls[100]{\scshape\MakeLowercase{#1}}}
\titleformat{\section}
{}
{\textsc{\MakeLowercase{\thesection}}}
{1em}
{\smallcaps}

\begin{document}
\section{This is my section title}
\end{document}


gives the following result

The \MakeLowercase around \thesection is to cope with possible appendices.

-
So this also holds for using it as {\textsfsl text}, I guess? –  Werner Feb 10 '12 at 17:12
Of course not: \textsfsl will take an argument, so in your example only the first "t" would be affected by the font change. –  egreg Feb 10 '12 at 17:14
Fantastic! As always. :) –  Paulo Cereda Feb 10 '12 at 17:14
@egreg: Thanks. My question was more in line with what it seems the OP is after - switches rather than macros with arguments. –  Werner Feb 10 '12 at 17:16
@Werner Let's hope that the OP makes a clearer question, then. –  egreg Feb 10 '12 at 17:32

I think you are rather looking for old font command behaviour like \bf, \tt etc. These "old" style commands can be defined using:

 \DeclareOldFontCommand{\mytt}{\sffamily\slshape}{\mathtt}


And here is a minimal as how to use them, the first parameter is the command name, followed by the normal text font declarations in the second argument. The last parameter is the math alphabet to be triggered in math mode.

\documentclass{article}
\DeclareOldFontCommand{\mytt}{\sffamily\slshape}{\mathtt}
\begin{document}
{\mytt this is a test} and this is normal.
\end{document}


The command is defined in the LaTeX kernel ltfntcmd.dtx. Definitions can also be found in all of LaTeX classes such as book and article. These type of commands have many limitations. Rather define your own semantic macros. Ideally a (La)TeX document should not have any typesetting commands in them. For example, let us say that you want to define all Ship names to be typeset as per normal English style guides that recommend they be typeset in italic. Define a macro such as:

\newcommand{\ship}[1]{\textit{#1}\xspace}


The advantage of this approach is that you can modify the macro to add them to an index as well automatically.

-
@Yannins Lazarides, the problem is that both the definition of the macro only works with switches, in the same way as \DeclareTextFontCommand. I'll rewrite the question more specifically, as required by egreg. –  henrique Feb 10 '12 at 18:13
@henrique Maybe we misunderstood you, but if you define a \myrm as well you would be able to do what you want alternative within macros \def\mymacro{{\mytt #1}} will work as well and contain its effects only within the macro. Is this what you want? –  Yiannis Lazarides Feb 10 '12 at 18:18

This one works as switch:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[osf]{libertine}
\usepackage[letterspace=10]{letterspace}

\newcommand{\smallcaps}{\lsstyle\scshape}

\begin{document}

This is normal with some Uppercase Letters.

\smallcaps these are real smallcaps.

\normalfont This is normal with some Uppercase Letters.

{\smallcaps and these are smallcaps, again.}

\end{document}


Note, that you have to give up the use of \MakeLowercase and have therefore to mind, that you write afterwards all in small case letters. \textls is replaced by \lsstyle; the letterspace factor must be given as a package option. You should use the switch only inside of groups, because the letterspacing is otherwise not resetted, compare the first with the third sentence an my example.

-