有没有类似 的简短版本\emph
?我想要的是与\lstShortInline
或shortvrb
提供的内容相当的东西,只是不是针对\verb
命令而是针对\emph
命令。
例如,而不是This is an \emph{important} test.
,我想写This is an |important| test.
或类似的东西。
答案1
\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}
答案2
你可以这样做:
\catcode`\|=\active
\def|#1|{\emph{#1}}
然后您就可以|text|
像使用一样使用\emph{text}
。
答案3
到目前为止给出的所有答案都捕获了一个论点,从而永久固定了论点标记的类别代码。如果您希望逐字文本与强调文本混合,这将不起作用。它们也不是\protected
,因此它们在标题或脚注中不起作用。
以下用途|+
...文本... +|
分隔强调的文本,适用于我刚刚描述的情况(同时满足您的原始要求)。我发现使用“ |+
”和“ +|
”之类的快捷方式更好,因为它允许您轻松地将“ |
”插入文档中。否则,您需要编写一些代表“ |
”原始含义的控制序列。
\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}
以上内容生成以下文档: