Tabularx 中的 Mintinline / 在 detokenize 中删除空格

Tabularx 中的 Mintinline / 在 detokenize 中删除空格

memoir我使用 Minted 包、TeXlive 2021 和 TeXmaker在课堂上编写了 LaTeX 手册。

\mintinline为了在 Tabularx X-type 列中插入一些代码,我使用了这个诡计并且它可以工作。但它会产生一个“去标记化逐字命令”,其中去标记化命令名称后面有空格,我想删除它们(或任何其他不需要的空格)。此外,我想保留与\mintinlinetabularx 环境之外的代码相同的形式。

为此,我尝试了很多解决方案(这个使用明显无效的宏\detokenizeplus那些),但它们都不起作用(或者我只是太糟糕了),无论是在 tabularx 环境内部还是外部。最接近的解决方案是使用tokcycle 包和他的宏\altdetokenize,至少在 tabularx 环境之外工作。

为了继续,我重写了宏的定义mintinline,就像下面的 MWE 一样(参见注释)。它消除了编译时的错误,我得到了想要的mintinline结果,但我不明白为什么它会保留那些不需要的空格(我想这超出了我的 LaTeX 技能),我需要你的帮助:

\documentclass[twoside,a4paper,11pt]{memoir}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{minted}
\usepackage{tokcycle}

\newif\ifmacro
\newcommand\altdetokenize[1]{\begingroup\stripgroupingtrue\macrofalse
  \stripimplicitgroupingcase{-1}%
  \tokcycle
    {\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi
     \detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi}
    {\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak}
    {\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi
     \allowbreak\detokenize{##1}\macrotrue}}
    {\hspace{0pt plus 3em minus .3ex}}
    {#1}%
  \unskip
\endgroup}

\makeatletter
\renewrobustcmd{\mintinline}[2][]{%
  \begingroup
  \setboolean{minted@isinline}{true}%
  \minted@configlang{#2}%
  \setkeys{minted@opt@cmd}{#1}%
  \minted@fvset
  \begingroup
  \let\do\@makeother\dospecials
  \catcode`\{=1
  \catcode`\}=2
  \catcode`\^^I=\active
  \@ifnextchar\bgroup
    {\minted@inline@iii}%
    {\catcode`\{=12\catcode`\}=12
      \minted@inline@i}}
\def\minted@inline@i#1{%
  \endgroup
  \def\minted@inline@ii##1#1{%
    \minted@inline@iii{##1}}%
  \begingroup
  \let\do\@makeother\dospecials
  \catcode`\^^I=\active
  \minted@inline@ii}
\ifthenelse{\boolean{minted@draft}}%
  {\renewcommand{\minted@inline@iii}[1]{%
    \endgroup
    \begingroup
    \minted@defwhitespace@retok
    \everyeof{\noexpand}%
    \endlinechar-1\relax
    \let\do\@makeother\dospecials
    \catcode`\ =\active
    \catcode`\^^I=\active
    \xdef\minted@tmp{\scantokens{#1}}%
    \endgroup
    \let\FV@Line\minted@tmp
    \def\FV@SV@minted@tmp{%
      \FV@Gobble
      \expandafter\FV@ProcessLine\expandafter{\FV@Line}}%
    \ifthenelse{\equal{\minted@get@opt{breaklines}{false}}{true}}%
     {\let\FV@BeginVBox\relax
      \let\FV@EndVBox\relax
      \def\FV@BProcessLine##1{\FancyVerbFormatLine{##1}}%
      \BUseVerbatim{minted@tmp}}%
     {\BUseVerbatim{minted@tmp}}%
    \endgroup}}%
  {\renewcommand{\minted@inline@iii}[1]{%
    \endgroup
    \minted@writecmdcode{#1}%
    \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
    \setcounter{minted@FancyVerbLineTemp}{\value{FancyVerbLine}}%
    \ifx\@footnotetext\TX@trial@ftn% <===== test if the macro is inside tabularx environment
    \altdetokenize{\minted@pygmentize{\minted@lang}}% <===== detokenize the token to avoid error (and supposed without unwanted space but it won't work)
    \else
    \minted@pygmentize{\minted@lang}% <===== normal behavior if outside of 
    \fi
    \setcounter{FancyVerbLine}{\value{minted@FancyVerbLineTemp}}%
    \endgroup}}
\makeatother

\begin{document}

Text with \mintinline{latex}{\inlinecode\without\space\after\command}.\\


\begin{tabularx}{\linewidth}{ X }
\toprule
 \mintinline{latex}{\inlinecode\without\space\after\command}    \\
\bottomrule
\end{tabularx}

\end{document}

如果您能给我提供一个优雅的解决方案,我将不胜感激!

相关内容