Search code examples
latextex

Use \newcommand without the text styles added to it


This is a weird question and I don't know if I was able to formulate it that well.

So, I declare a newcommand somewhere like this:

\newcommand{\examplecommand}{\textbf{\textit{exampletext}}}

It'll be used quite many times this way.

I'm trying to also use it in one place in the document, wanting to format it in a different way (with \textsc{}, and none of the bolding and italicization)

I tried doing stuff like

\textsc{\examplecommand}

But that doesn't work, it still somehow prioritizes the formatting in the command declaration.

How can I use the command without changing the declaration, but with different formatting?

EDIT (MWE):

document.tex
--------------------
\documentclass{book}

\usepackage{fancyhdr}
\usepackage{blindtext}

\newcommand{\booktitle}{} % create it empty at first, so that the files can change it

\newcommand{\textbfit}[1]{\textbf{\textit{#1}}} % combine bold and italic in one

\fancypagestyle{plain}{
 \fancyhf{}
 \fancyhead[RO,RE]{\textsc{\booktitle}}
 \renewcommand{\headrulewidth}{2pt}
}

\begin{document}
\pagestyle{plain}

\chapter{example1}
\input{doc1}

\chapter{example2}
\input{doc2}

\chapter{example3}
\input{doc3}

\end{document}
doc1.tex
--------------------
\renewcommand{\booktitle}{\textbfit{``Title 1''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc

\blindtext
doc2.tex
--------------------
\renewcommand{\booktitle}{\textbfit{``Title 2''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc

\blindtext
doc3.tex
--------------------
\renewcommand{\booktitle}{\textbfit{``Title 3''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc

\blindtext

The reason I want to be able to do this:

I have 12 documents that I wrote in a single-document form, and now I wish to make them work both in the single-document form (all of them having a separate .tex file that compiles them as a single document), and a form that puts them all into a large file.

I was using that \booktitle command across all of them, since they are all similar in general structure. And now when I wanted to also convert them into book form, I discovered I can use it to change the top right corner text inside the header (see that the definition of the plain fancypagestyle uses that command inside its fancyhead), but, in there, I want to use it with \textsc

The reason I don't want to change my command is because it means changing it across all of the documents inside, and I was just thinking I could do \textsc{\booktitle} and be done with it


Solution

  • You could temporarily switch off your \textbfit command:

    \documentclass{book}
    
    \usepackage{fancyhdr}
    \usepackage{blindtext}
    
    \newcommand{\booktitle}{} % create it empty at first, so that the files can change it
    
    \newcommand{\textbfit}[1]{\textbf{\textit{#1}}} % combine bold and italic in one
    
    \fancypagestyle{plain}{
     \fancyhf{}
     \fancyhead[RO,RE]{
      \begingroup
        \let\textbfit\relax
        \textsc{\booktitle}
      \endgroup
     }
     \renewcommand{\headrulewidth}{2pt}
    }
    
    \begin{document}
    \pagestyle{plain}
    
    \chapter{example1}
    
    \renewcommand{\booktitle}{\textbfit{``Title 1''}}
    \booktitle\ is a very nice book, it's really great, buy it etcetcetc
    
    \blindtext
    
    
    \end{document}