5

I need to use Bold or Italic a particular word through my latex document, where ever it is used, without explicitly mentioning textbf{} and textit{} on each of them. How can I do that?

A good analogy would be, CSS to HTML tags.

Is it possible to this in Latex?

  • \newcommand{\mystrangeword}{\textbf{particularword}\xspace} for example, use \mystrangeword then. -- and yes, \xspace has its issues ;-) –  Oct 07 '15 at 15:41

3 Answers3

7

If it's a similar word each time you can create a command:

\newcommand{\foo}{\textbf{foo}\xspace}

Another solution is to create shorter tag for \textbf or \textit:

\newcommand{\tit}[1]{\textit{#1}}
\newcommand{\tbf}[1]{\textbf{#1}}

Those solutions suppose that you have no particular command called the same way or an error will be raised.

Romain Picot
  • 6,730
  • 4
  • 28
  • 58
7

Here is \mymarkup in a starred and unstarred variant. The unstarred variant gives \textbf{...} and the starred one is responsible for italic version. The double starred version gives bold and italic and the optional parameter changes the text:

\documentclass{article}



\usepackage{xparse}

\NewDocumentCommand{\mymarkup}{ssO{myword}}{%
  \IfBooleanTF{#1}{%
    \IfBooleanTF{#2}{%
      {\bfseries\itshape #3}%
    }{%
      \textit{#3}%
    }%
  }{%
    \textbf{#3}%
  }%
}


\begin{document}
\mymarkup\ is better than \mymarkup*\, which is followed by \mymarkup**\ again

\mymarkup[Don't use xspace]\ is better than \mymarkup*[Don't use xspace]\, which is followed by \mymarkup**[Don't use xspace]\ again


\end{document}

enter image description here

6

Just for variety, here's a LuaLaTeX-based solution. Note that it doesn't require you to modify the body of the document in any way. The word to be rendered in bold-italics throughout the document should be listed as the second argument of the string.gsub function.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode}
\begin{luacode}
function highlight_word ( line )
   return string.gsub ( line, "myparticularword", "{\\bfseries\\itshape %0}" )
end
luatexbase.add_to_callback ( "process_input_buffer", highlight_word, "highlight_word" )
\end{luacode}

\begin{document}
An interesting story about myparticularword and its history \dots
\end{document}
Mico
  • 506,678