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.
Glad to hear that._
\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.
Set the features by hand:
\documentclass{article}
\usepackage{fontspec}
\setmainfont{SansForgetica}[
Path=./,
Extension=.otf,
UprightFont=*-Regular,
BoldFont=*-Regular,
BoldFeatures={FakeBold=3},
ItalicFont=*-Regular,
ItalicFeatures={FakeSlant=0.3},
BoldItalicFont=*-Regular,
BoldItalicFeatures={FakeBold=3,FakeSlant=0.3},
]
\begin{document}
Abc def
\textit{Abc def}
\textbf{Abc def}
\textit{\textbf{Abc def}}
\end{document}
Note: I use Path=./
in order to load the font from a working directory, as I don't want to mix the beast with my system fonts. Your setup may differ.
Best Answer
Marc van Dongen gave a great answer. I'll throw in another reason:
\it
and\bf
do not play well together. That is, they do not nest as one would intuitively expect:Whereas
\textit
and\textbf
do play well together:This is nice. However, you may notice that it still fails to handle nested style adjustments to small caps, since the Computer Modern fonts do not contain slanted or bold small caps:
If this is a problem for you, then use the
slantsc
package in combination with thelmodern
package.slantsc
provides, among other things,\rmfamily
(roman),\ttfamily
(typewriter/teletype),\sffamily
(sans-serif),\bfseries
(boldface),\itshape
(italics),\slshape
(slant/oblique), and\scshape
(small caps). With these, small caps can obtained in slanted form:As a bonus,
slantsc
fixes\textsl
to behave properly with\textsc
, so you can continue using those if you like.Alas, I haven't yet found a package which fixes the behavior of nested instances of
\textit
. In typesetting, when you nest italics, you're supposed to come back out of italics to roman. For example, the word "Titanic" below is in nested italics (which should ideally render as roman, not italics):As a workaround, one can usually write
\textrm
to temporarily return to non-italics in those cases, but of course this is only valid if you know the exact number of nested italic levels, which may not always be the case, especially inside a macro.Update:
As others have pointed out,
\textit
and\textsl
do automatic italic correction, whereas\it
,\itshape
,\sl
, and\slshape
do not. Thus, you can write\textit{stuff}
, but you must write{\it stuff\/}
or{\itshape stuff\/}
to get the same effect.