# How to set italics and bold shortcuts like *bold* or _italic_

I remember seeing a possible solution here, but can't find it. Is there any way to simplify bold and italics so that instead of \textbf{bold text} and \textit{italics text} I use simple marks like (such as in Word) bold text

*bold text*


and italics

_italics_


for the whole document?

-
Do you use mathematics? If not, the _ should not be a problem, I'll post an answer below. If yes, it is a serious problem related to \catcodes. However, the star * is a problem since you cannot make it \active and work in commands like \section* both at once. – yo' Feb 26 '13 at 12:24
(La)TeX is for humans (you, me, others here) to read and understand. Like the markup here. But people would get mightily confused if instead of (familiar) \textbf{junk} they start encountering **junk** in your text. – vonbrand Feb 26 '13 at 19:54
Perhaps you saw this answer? – Hendrik Vogt Feb 28 '13 at 9:17
@vonbrand, I agree. I just wanted to know if something like that might work. Now I see it doesn't work properly. But "familiarity" is relative, and so is understanding. If I showed someone who isn't familiar with Latex the macro \textbf, it wouldn't make much sense either, until he figures it out. But I suggest you tone down your implied sarcasm in "\textbf{junk}". – Joseph Feb 28 '13 at 13:55
@vonbrand, that's ok. And yes, I agree something like \texbf is more logical. Maybe I need to use too much \textit for transliterations... – Joseph Feb 28 '13 at 19:41

For the underscore, the following works:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}

\catcode\_=\active
\protected\def_{\begingroup\itshape\aftergroup\/\let_\endgroup}

\begin{document}

Hello \textit{World!} How are you?

Hello _World!_ How are _you?

I'm fine._ And you?

_I'm fine, too.

\end{document}


However, it is a bit crazy and unstable. If you want to use it only for short texts (not spanning multiple paragraphs), the following would be better. (It doesn't work in the above example, since there we span multiple pagraphs. In real, it will throw an error if you put odd number of _ in one paragraph.)

\catcode\_=\active
\protected\def_#1_{\textit{#1}}


You can use the same ideas for the star. The problem is, that \section*{Text} will suddenly stop working. Variant 1:

\catcode\*=\active
\protected\def*{\begingroup\bfseries\let*\endgroup}


Variant 2:

\catcode\*=\active
\protected\def*#1*{\textbf{#1}}


If you don't use math at all, just use ^ instead of * and it should be ok.

How does it work: The primitive macro \catcode makes _ \active so that we can define it as any other command.

In Variant 1, we define it to (1) start a group (2) start italic text (3) add italic correction to the end of the italic text (4) make the one next _ end the group we started. By the end of the group, the re-definition of _ is forgotten so another _ will again start an italic text.

The Variant 2 is even simpler: When _ is found, a second _ is looked for, end everything inbetween is put into \textit.

The \protected directive makes sure that _ is written as _ in the auxiliary files, which is necessary for it to behave correctly.

-
You should use \protected\def_ in order to avoid quirks in moving arguments. And probably an \ifmmode switch in the definition. – egreg Feb 26 '13 at 13:15
@egreg thanks pal, I oversaw this. And I can make it work in maths as the subscript symbol, but I doubt it is a good idea to make _ do two completely different things in one document. – yo' Feb 26 '13 at 13:55
not only \section* but any star-variant command... – jfbu Feb 26 '13 at 14:03
@user700902 I leave that to the intuition of the users ;) And you just earned one nice duck for finding it out, you can claim it in the chat ;) – yo' Feb 26 '13 at 14:05