如何将包含 # 的 URL 传递给宏而不引发错误?

如何将包含 # 的 URL 传递给宏而不引发错误?

我经常需要使用带有#字符的 URL,而标签当然会导致编译错误。到目前为止,我已经使用了两种方法来解决这个问题。一种是手动转义它们(将它们更改为 ,\#另一种是使用 URL 缩短器。我班上的学生并不总是记得做这两件事,我想要一种简单地将 URL 传递给宏的方法。我发现\AtBeginEnvironment当我定义使用 URL 的环境时,我可以使用钩子来修复问题,但钩子机制不适用于环境的宏形式(可能是 的产物tcolorbox)。为什么会这样?

我花了几个小时在这个网站上寻找解决方案,但似乎没有什么效果。解决方案解释如下https://tex.stackexchange.com/a/554190/218142https://tex.stackexchange.com/a/554200/218142看起来很有希望,但对我来说没用。

有没有一种干净的方法来传递 URL,而不必每次都手动修复它或每次都缩短它?

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article}

\begin{filecontents}[overwrite,noheader]{demo.py}
Hello, world!
\end{filecontents}

\usepackage[most]{tcolorbox}
\usepackage{hyperref}

\AtBeginEnvironment{codeenv}{\catcode`\#=12}
\NewTCBListing[auto counter]{codeenv}
  { O{} D(){glowscript.org} m }{%
  breakable,%
  center,%
  %code = \newpage,%
  enhanced,%
  hyperurl interior = https://#2,%
  label = {gs:\thetcbcounter},%
  left = 8mm,%
  listing only,%
  listing style = tcblatex,%
  title = Listing: #3,%
  width = 0.9\textwidth,%
  {#1},
}%

\NewTCBInputListing[auto counter]{\codefile}
  { O{} D(){glowscript.org} m m }{%
  breakable,%
  center,%
  %code = \newpage,%
  enhanced,%
  hyperurl interior = https://#2,%
  label = {vp:\thetcbcounter},%
  left = 8mm,%
  listing file = {#3},%
  listing only,%
  listing style = tcblatex,%
  title = Listing: #4,%
  width = 0.9\textwidth,%
  {#1},%
}%

\begin{document}
This works using the hook.
\begin{codeenv}(www.glowscript.org/#/user/heafnerj/folder/Public/program/nhattest){My Program}
Hello, world!
\end{codeenv}

This works with a shortened URL.
\begin{codeenv}(t.ly/9i20){My Program}
Hello, world!
\end{codeenv}

%This does not work.
%\codefile(www.glowscript.org/#/user/heafnerj/folder/Public/program/nhattest){demo.py}{My Program File}

This works by escaping the \#.
\codefile(www.glowscript.org/\#/user/heafnerj/folder/Public/program/nhattest){demo.py}{My Program File}

This works with a shortened URL.
\codefile(t.ly/9i20){demo.py}{My Program File}
\end{document}

答案1

在这里,我添加了以下几行

\newcommand\codefile{\catcode`\#=12 \codefileauxA}
\NewDocumentCommand\codefileauxA{ O{} D(){glowscript.org} m m }{%
  \codefileauxB[#1](#2){#3}{#4}%
  \catcode`\#=6 
}

并将原先的名字重新命名\codefile

\NewTCBInputListing[auto counter]{\codefileauxB}

#这样做的效果是仅在调用时\codefileauxA(吸收实际参数的地方)更改 catcode ,并在\codefileauxB完成后将其改回。

妇女权利委员会:

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article}

\begin{filecontents}[overwrite,noheader]{demo.py}
Hello, world!
\end{filecontents}

\usepackage[most]{tcolorbox}
\usepackage{hyperref}

\AtBeginEnvironment{codeenv}{\catcode`\#=12}
\NewTCBListing[auto counter]{codeenv}
  { O{} D(){glowscript.org} m }{%
  breakable,%
  center,%
  %code = \newpage,%
  enhanced,%
  hyperurl interior = https://#2,%
  label = {gs:\thetcbcounter},%
  left = 8mm,%
  listing only,%
  listing style = tcblatex,%
  title = Listing: #3,%
  width = 0.9\textwidth,%
  {#1},
}%

\newcommand\codefile{\catcode`\#=12 \codefileauxA}
\NewDocumentCommand\codefileauxA{ O{} D(){glowscript.org} m m }{%
  \codefileauxB[#1](#2){#3}{#4}%
  \catcode`\#=6 
}
\NewTCBInputListing[auto counter]{\codefileauxB}
  { O{} D(){glowscript.org} m m }{%
  breakable,%
  center,%
  %code = \newpage,%
  enhanced,%
  hyperurl interior = https://#2,%
  label = {vp:\thetcbcounter},%
  left = 8mm,%
  listing file = {#3},%
  listing only,%
  listing style = tcblatex,%
  title = Listing: #4,%
  width = 0.9\textwidth,%
  {#1},%
}%

\begin{document}
This works using the hook.
\begin{codeenv}(www.glowscript.org/#/user/heafnerj/folder/Public/program/nhattest){My Program}
Hello, world!
\end{codeenv}

This works with a shortened URL.
\begin{codeenv}(t.ly/9i20){My Program}
Hello, world!
\end{codeenv}

This does not work.
\codefile(www.glowscript.org/#/user/heafnerj/folder/Public/program/nhattest){demo.py}{My Program File}

This works by escaping the \#.
\codefile(www.glowscript.org/\#/user/heafnerj/folder/Public/program/nhattest){demo.py}{My Program File}

This works with a shortened URL.
\codefile(t.ly/9i20){demo.py}{My Program File}
\end{document}

在此处输入图片描述

相关内容