17

Is there something like a short version of \emph? What I want is something comparable to \lstShortInline or what the shortvrb provides, just not for the \verb command but for the \emph command instead.

E.g. instead of This is an \emph{important} test., I want to write This is an |important| test. or something similiar.

1
  • 1
    Do you want to write only the short version, or do you want to have the short version in the source? The former is a matter of the editing environment...
    – DevSolar
    Jan 9, 2012 at 13:37

3 Answers 3

30
\documentclass{article}
\catcode`\|=13 % make | active
\def|#1|{\emph{#1}}
\begin{document}
This is an \emph{important} test.
This is an |important| test.
\end{document}
0
25

You could just go:

\catcode`\|=\active
\def|#1|{\emph{#1}}

Then you can use |text| as if it were \emph{text}.

11

All the answers given so far capture an argument, fixing the category codes of the argument tokens forever. That will not work if you want verbatim text mixed with your emphasized text. They are also not \protected, so they won't work in a caption or footnote.

The below uses |+ ...text... +| to delimit emphasized text and works in the cases I just described (as well as meeting your original requirements). I find it better to use a shortcut like "|+" and "+|" because it allows you to insert "|" into your document easily. Otherwise, you need to make up some control sequence representing the original meaning of "|".

\documentclass{article}
%% Justin Bailey
%% [email protected]
%% January, 2012
\newtoks\toinsert %% scratch token register
\makeatletter 
%% Capture meaning of | and + 
\let\emphbar=| \let\emphplus=+ 
\catcode`\|=\active \catcode`\+=\active
\protected\def\startEmphA{\futurelet\next\startEmphB}
%% See if | is followed by +, indicating we are starting empahsized
%% text. If so, begina new group and run \startEmphC. If not, put |
%% back back into stream.
\def\startEmphB{\toinsert={\emphbar}%%
  \ifx\next+\toinsert={\bgroup\startEmphC}\fi
  \the\toinsert} %%
\def\startEmphC{\ifmmode%%
  \let \math@bgroup \relax%%
  \def \math@egroup {\let \math@bgroup \@@math@bgroup%%
    \let \math@egroup \@@math@egroup}%%
  \mathit\relax%%
  \else%%
  \itshape\fi\@gobble}%%
\protected\def\stopEmphA{\futurelet\next\stopEmphB}
%% See if + is followd by a |, indicating the end of emphasized
%% text. If it is, end the group and gobble the | token. Otherwise,
%% put a + back into the stream. 
\def\stopEmphB{\toinsert={\emphplus}%%
  \ifx\next|\toinsert={\egroup\@gobble}\fi%%
  \the\toinsert}
%% Associate | and + with startEmphA and stopEmphA. A more
%% robust implementation would put these in a macro so
%% we could turn the definitions of | and + on or off.
\protected\def|{\startEmphA} \protected\def+{\stopEmphA}
\makeatother
\usepackage[paperheight=3in,paperwidth=3in]{geometry}
\begin{document}
\pagestyle{empty}
%% Basic test
\tracingmacros=2\tracingcommands=2This is an |+important+| test.

%% Use special characters inside emphasize environment. Note
%% that | becomes an em-dash in this font.
This is an |+important (+x|y+)+| test.

%% Test in math mode
This is an $|+important+|$ test.

%% Test in a caption
\begin{figure}
This is an |+important+| test.
\caption{This is an |+important+| test.}
\end{figure}

%% With verbatim text.
This is an |+important \verb=$test$=+|.

%% Nested emphasis
This is an |+important, |+important+|+| test.
\end{document}

The above produces the following document:

Example text.

2
  • I do actually have a quite short version here: gist.github.com/1584374 It's a little different, but works great for me.
    – Michael
    Jan 11, 2012 at 10:45
  • That works in most cases but its not robust enough to handle verbatim text. For example, This is an /important \verb=$test$=/ test. will fail with an error. Similarly, it will fail in a \caption unless you add \protected before the definition of \def/#1/{...}. Jan 11, 2012 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .