Text FormattingAutomatically format keywords (bold, italicize, etc)

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Automatically format keywords (bold, italicize, etc)

Post by ptrcao »

Is there a command or package that will allow me to automatically format keywords (bold, italicize, etc)? How do I do this?
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Frits
Posts: 169
Joined: Wed Feb 02, 2011 6:02 pm

Automatically format keywords (bold, italicize, etc)

Post by Frits »

What do you mean with keywords?

I'm guessing you want certain words in your document having a specific format:

Code: Select all

\newcommand{\keyword}{\textit{keyword}}
\newcommand{\keyword}{\textbf{keyword}}
With the first command, LaTeX prints the word 'keyword' in italic when you insert the command \keyword (so with the slash). The latter one does the same, but then in boldface.
howtoTeX.com - Your LaTeX resource site (Tips, Tricks, Templates and more!)
Follow howtoTeX on twitter
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Automatically format keywords (bold, italicize, etc)

Post by localghost »

ptrcao wrote:Is there a command or package that will allow me to automatically format keywords (bold, italicize, etc)? How do I do this?
Are we talking about a code listing or just about emphasis of words?


Thorsten
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Automatically format keywords (bold, italicize, etc)

Post by ptrcao »

By keyword, I just mean words of my choice which I'd like to format, such as book titles, etc. which need to be italicised.

Isn't there a function that will search for all instances of the keyword and format it according to my desire?

This approach

Code: Select all

\newcommand{\keyword}{\textit{keyword}}
\newcommand{\keyword}{\textbf{keyword}}
works too, but LaTeX then ignores whitespaces after it for some reason. I'm not sure why. I can include a whitespace after the keyword such as:

Code: Select all

\newcommand{\keyword}{\textit{keyword }}
But I'm still curious to know if there is a search and format function in LaTeX.
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
Frits
Posts: 169
Joined: Wed Feb 02, 2011 6:02 pm

Automatically format keywords (bold, italicize, etc)

Post by Frits »

Yep, the whitespace can be quite annoying. A possible fix would be:

Code: Select all

usepackage{xspace}
\newcommand{\keyword}{\textit{keyword\xspace}}
Last edited by Frits on Sun Feb 20, 2011 2:03 pm, edited 1 time in total.
howtoTeX.com - Your LaTeX resource site (Tips, Tricks, Templates and more!)
Follow howtoTeX on twitter
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Automatically format keywords (bold, italicize, etc)

Post by ptrcao »

What about titles of books like "The Lord of The Rings", which have multiple words interrupted by spaces? You can't have

Code: Select all

\newcommand{\The Lord of The Rings}{\textit{The Lord of The Rings}}
because only the "The" in this instance would be included in the command.
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
Frits
Posts: 169
Joined: Wed Feb 02, 2011 6:02 pm

Automatically format keywords (bold, italicize, etc)

Post by Frits »

I edited my post above for a fix on that whitespace at the end.

For your Lord of Rings question: That's a nasty one. Isn't it possible (and easier) to define:

Code: Select all

\newcommand{\lotr}{\textit{The Lord of The Rings}}
howtoTeX.com - Your LaTeX resource site (Tips, Tricks, Templates and more!)
Follow howtoTeX on twitter
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Automatically format keywords (bold, italicize, etc)

Post by ptrcao »

Frits wrote:I edited my post above for a fix on that whitespace at the end.

For your Lord of Rings question: That's a nasty one. Isn't it possible (and easier) to define:

Code: Select all

\newcommand{\lotr}{\textit{The Lord of The Rings}}
Ah yes, how convenient, I see somebody came up with a function just for this very situation; it appears \xspace takes care of it all so you don't have to think about it.

I originally thought you could do

Code: Select all

\newcommand{\The-Lord-of-The-Rings}{\textit{The Lord of The Rings}}
or even

Code: Select all

\newcommand{\The_Lord_of_The_Rings}{\textit{The Lord of The Rings}}
but apparently these generate errors.

And right, your suggestion

Code: Select all

\newcommand{\lotr}{\textit{The Lord of The Rings}}
is definitely easier in this case, but I can think of some cases where I would like the LaTeX markup would more accurately resemble the typeset result.
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
Post Reply