如何删除 \detokenize 中标记后的空格?

如何删除 \detokenize 中标记后的空格?

以下是代码:

\documentclass{article}
\catcode`|=\active
\def|#1|{\texttt{\detokenize{#1}}}
\begin{document}
Hello, |\a\b|!
\end{document}

其渲染效果如下:

在此处输入图片描述

标记后的空格是错误的。我的代码有什么问题?

顺便说一句,使用\texttt只是为了实验。在我的实际案例中,我有一个更复杂的宏,它需要一个参数。

答案1

您想阅读以下论点v

在此处输入图片描述

\documentclass{article}
\usepackage{color}

\NewDocumentCommand\foo{v}{\fbox{\color{blue}\ttfamily#1}}

\catcode`\|\active\def|{\foo|}

\begin{document}

bbb |\a\b|!


\end{document}

答案2

使用选项shortverb,并且\MakeShortVerb|已经在https://tex.stackexchange.com/a/665018/2388所以这里有一个替代方案fancyvrb

\documentclass{article}
\usepackage{fancyvrb} 
\DefineShortVerb{\|}
\begin{document}

|\a\b!|

\end{document}

在此处输入图片描述

答案3

正如Ulrike Fischer所说:


\documentclass{article}

\usepackage{shortvrb}

\MakeShortVerb|


\begin{document}

Hello, |\a\b|!

\end{document}

答案4

将包包含shortvrb在您的 .tex 文件中。

(版权所有 1993-2022 LaTeX3 项目。)

\makeatletter
\def\MakeShortVerb{%
  \@ifstar
    {\def\@shortvrbdef{\verb*}\@MakeShortVerb}%
    {\def\@shortvrbdef{\verb}\@MakeShortVerb}}
\def\@MakeShortVerb#1{%
  \expandafter\ifx\csname cc\string#1\endcsname\relax
    \@shortvrbinfo{Made }{#1}\@shortvrbdef
    \add@special{#1}%
    \expandafter
    \xdef\csname cc\string#1\endcsname{\the\catcode`#1}%
    \begingroup
      \catcode`\~\active  \lccode`\~`#1%
      \lowercase{%
      \global\expandafter\let
         \csname ac\string#1\endcsname~%
      \expandafter\gdef\expandafter~\expandafter{\@shortvrbdef~}}%
    \endgroup
    \global\catcode`#1\active
  \else
    \@shortvrbinfo\@empty{#1 already}{\@empty\verb(*)}%
  \fi}
\def\DeleteShortVerb#1{%
  \expandafter\ifx\csname cc\string#1\endcsname\relax
    \@shortvrbinfo\@empty{#1 not}{\@empty\verb(*)}%
  \else
    \@shortvrbinfo{Deleted }{#1 as}{\@empty\verb(*)}%
    \rem@special{#1}%
    \global\catcode`#1\csname cc\string#1\endcsname
    \global \expandafter\let \csname cc\string#1\endcsname \relax
    \ifnum\catcode`#1=\active
      \begingroup
        \catcode`\~\active   \lccode`\~`#1%
        \lowercase{%
          \global\expandafter\let\expandafter~%
          \csname ac\string#1\endcsname}%
      \endgroup \fi \fi}
\def\@shortvrbinfo#1#2#3{%
  \PackageInfo{shortvrb}{%
     #1\expandafter\@gobble\string#2 a short reference
                                          for \expandafter\string#3}}
\def\add@special#1{%
  \rem@special{#1}%
  \expandafter\gdef\expandafter\dospecials\expandafter
    {\dospecials \do #1}%
  \expandafter\gdef\expandafter\@sanitize\expandafter
    {\@sanitize \@makeother #1}}
\def\rem@special#1{%
  \def\do##1{%
    \ifnum`#1=`##1 \else \noexpand\do\noexpand##1\fi}%
  \xdef\dospecials{\dospecials}%
  \begingroup
    \def\@makeother##1{%
      \ifnum`#1=`##1 \else \noexpand\@makeother\noexpand##1\fi}%
    \xdef\@sanitize{\@sanitize}%
  \endgroup}
\makeatother

相关内容