14

I currently use latex newcommand to store (and format) "variables", like so:

\newcommand{\someVariable}{\emph{value}}

I do this for key words that I want to be formatted a certain way every time they appear, by typing \someVariable{}

Is there a way to remove the formatting from \someVariable{} for certain times that I do NOT want any special formatting, something like \removeFormatting{\someVariable{}}.

Of course I could just manually type the text value, but I prefer to keep it in a command so that I can change all occurrences of it easily.

15

You can use a higher level definition:

\usepackage{xparse}
\NewDocumentCommand{\definevariable}{mmm}{%
  \NewDocumentCommand{#1}{s}{\IfBooleanTF{##1}{#3}{#2{#3}}}%
}

\definevariable{\someVariable}{\emph}{value}
\definevariable{\otherVariable}{\quotedtextbf}{x}

\newcommand{\quotedtextbf}[1]{``\textbf{#1}''}

Now calling \someVariable will print "value" applying \emph, while \someVariable* will print value without special formatting. Similarly, \otherVariable will print “x”, while \otherVariable* will print a simple x. Note how we can apply additional formatting such as quotes and boldface.

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\definevariable}{mmm}{%
  \NewDocumentCommand{#1}{s}{\IfBooleanTF{##1}{#3}{#2{#3}}}%
}

\definevariable{\someVariable}{\emph}{value}
\definevariable{\otherVariable}{\quotedtextbf}{x}

\newcommand{\quotedtextbf}[1]{``\textbf{#1}''}

\begin{document}
\someVariable{} \otherVariable{}

\someVariable* \otherVariable*

\end{document}

will print

valuex
value x

  • This is almost exactly what I want... the only thing is that it won't always be \emph, it could be anything really... \textbf, \textsc... sometimes I even add quotation marks to the text: \newcommand{\someFunction}[1]{``\textsc{#1}''} – Joshua Spence Apr 12 '12 at 13:26
  • @JoshuaSpence, how about pass three arguments \NewDocumentCommand{\definevariable}{mmm}, where #2 is the unformatted version, and #3 has the formatting (i.e. substitute #3 in place of \emph{#2}). E.g \definevariable{\someVariable}{value}{"\textsc{value}"} – huon Apr 12 '12 at 13:43
  • @JoshuaSpence See edited answer which is just dbaupp's idea. – egreg Apr 12 '12 at 15:31
9

You can temporarily set the formatting command(s) to \relax. If you do this within a group, they will automatically be restored afterwards.

\documentclass{article}
\newcommand\goat{\emph{goat}}
\newcommand\noemph[1]{\bgroup\let\emph\relax#1\egroup}
\begin{document}
\goat
\noemph{\goat}
\goat
\end{document}
2

You could define a "conditional emphasis" macro -- such as the macro \emphvar in the MWE below -- as follows:

  • if it's given just one argument, set off in curly braces, it'll emphasize that argument; and
  • if it's also given an optional argument, set off in square brackets with a value of "0" (zero), it will not emphasize its main argument.

With the code given in the MWE below, you could specify \emphvar{Value} to italicize the string "Value", whereas \emphvar[0]{Value} would selectively disable the emphasizing action of the macro. The \emphvar macro can thus also be used to affect the appearance of keywords in your document's preamble.

\documentclass{article}
\newcommand{\emphvar}[2][]{%    % macro has two args; first arg is optional
   \ifx#10{#2}\else\emph{#2}\fi}
\newcommand{\Keywordi}{\emphvar{FirstKeyword}}
\newcommand{\Keywordii}{\emphvar[0]{SecondKeyword}}
\begin{document}
\emphvar{Value}    % without optional arg.: emphasize macro's argument
\emphvar[0]{Value} % with intended optional arg., viz., "0": no emphasis
\emphvar[x]{Value} % with any other value for the optional arg., the macro's
                   % behavior is the same as if no optional argument were present

\Keywordi{} \Keywordii
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.