The basic idea of \DefineShortVerb
is that it makes the character in the argument active (\catcode`#1=\active
), so that the character behaves like a macro. Then it defines that character to be a verbatim command using the "lowercase trick". You can change that to define the character to a custom macro.
Here's a \DefineShortCommand<char>{<definition>}
macro which defines the <char>
to expand to <definition>
. This eventually does something like \def<char>#1<char>{<definition>}
, in which <char>
is an active character token. In the <definition>
, #1
is the argument, grabbed between the first and the next (brace-balanced) <char>
.
\documentclass{article}
\def\DefineShortCommand#1{%
\catcode`#1=\active
\begingroup
\lccode`\~=`#1
\lowercase{\endgroup
\def~##1~}}
\begin{document}
\DefineShortCommand\`{\textit{#1}}
`this` and \textit{this} should be the same.
\end{document}
Take care not to activate "dangerous" characters and beware with possible conflicts with babel
.
To use with Verbatim
, the character `
is especially painful because this character is reset by LaTeX's \@noligs
to avoid that it forms ligatures in verbatim mode. fancyvrb
uses \@noligs
after the user's catcode settings, so anything you change to the character `
is overridden. You need to remove it from the verbatim ligature list:
\documentclass{article}
\usepackage{fancyvrb}
\def\DefineShortCommand#1{%
\catcode`#1=\active
\begingroup
\lccode`\~=`#1
\lowercase{\endgroup
\def~##1~}}
\begin{document}
\makeatletter
\def\verbatim@nolig@list{\do\<\do\>\do\,\do\'\do\-}
\def\backtickit{\DefineShortCommand\`{\textit{##1}}}
\makeatother
\begin{Verbatim}[codes={\backtickit}]
x=1/sqrt(z**2) <- This is an `equation`
\end{Verbatim}
\end{document}
As Mico noted, when two `
are used (like in ``should''
) you usually don't want to apply the special formatting. To detect this case you can check that the argument of active-`
is empty and then use the original `
character instead.
Suppose you have a \tlifempty{<token list>}{<empty>}{<not empty>}
(shown below) conditional, then this could be accomplished with:
\DefineShortCommand\`{%
\tlifempty{#1}
{\char96\char96\relax}
{\textit{#1}}}
(96 is the ASCII code for `
).
To go an extra mile and allow the eventual usage of `
(or other character that you happen to use with \DefineShortCommand
) in an expansion-only context, you need to save the original meaning of `
and then retrieve it later with an auxiliary macro.
In the code below, \DefineShortCommand
saves the original meaning of the argument as (for example `
) \the@char@96
. Later you can retrieve it with \thechar{`}
. As an extra safety, the code does some normalisation so that both `
or \`
retrieve the same character:
\documentclass{article}
\makeatletter
\def\DefineShortCommand#1{%
\expandafter\edef\csname the@char@\number`#1\endcsname
{\unless\if\@backslashchar\string#1%
\string#1\else\expandafter\@gobble\string#1\fi}%
\catcode`#1=\active
\begingroup
\lccode`\~=`#1
\lowercase{\endgroup
\def~##1~}}
\def\thechar#1{\csname the@char@\number`#1\endcsname}
% Checking if the argument is empty
\def\tlifempty#1{%
\if\relax\detokenize{#1}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother
\begin{document}
\DefineShortCommand\`{%
\tlifempty{#1}
{\thechar{`}\thechar{\`}}
{\textit{#1}}}
`this` and \textit{this} ``should'' be the same.
\end{document}
\textit{}
and place the cursor between the braces.