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 ;-) – user31729 Oct 7 '15 at 15:41
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

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.

  • \xspace... ;-) – user31729 Oct 7 '15 at 15:43
  • @ChristianHupfer keyboard doesn't want to work all the time ;-) – Romain Picot Oct 7 '15 at 15:44
  • 2
    \i and \b are defined commands already – user31729 Oct 7 '15 at 15:49
  • Oh no! An xspace! – yo' Oct 7 '15 at 15:51
  • @yo' A wild xspace appears. xspace used \xspace, it’s super effective! – moewe Oct 7 '15 at 16:03
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}
  • You're into lua recently, hm??? ;-) – user31729 Oct 7 '15 at 15:58
  • @ChristianHupfer - Yup, you got me pegged... – Mico Oct 7 '15 at 15:58
  • 1
    I got you pegged? What have I done? :D – user31729 Oct 7 '15 at 16:02

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.