重命名 \href 命令以首先检查参数

重命名 \href 命令以首先检查参数

我想检查给定的 URL \href。为此,我必须重命名\href以执行一些代码。我认为解析参数时出了问题,因为代码进入了循环并且超出了 TeX 容量。

\documentclass{article}
\usepackage[left=1cm,includemp,right=1.0cm, marginparwidth=6cm]{geometry}

\usepackage{hyperref}

\RequirePackage{xparse}
\RequirePackage[unique=true]{bashful}
\RequirePackage{marginnote}

\let\hrefnormal\href
\DeclareDocumentCommand \href {o m m}{%
   \IfNoValueTF{#1}
   {\hrefnormal{#2}{#3}}
   {\hrefnormal[#1]{#2}{#3}}%
   \marginnote{URL: \splice{curl -IsS \detokenize{#2} 2>&1 | head -n 1 |  sed \detokenize{'s/HTTP\/1\.1 ... //'}}}
}




\begin{document}
\setlength{\parindent}{0pt}
\begin{center}
 {\huge\textbf{URL Validation}}
\end{center}

Hidden URLs:

\href{http://tex.stackexchange.com/test-404/}{This will give a 404 - Page Not Found}
\end{document}

答案1

\usepackage{letltxmacro}是必需的,因为\href它是一个带有可选参数的命令。

\documentclass{article}
\usepackage[left=1cm,includemp,right=1.0cm, marginparwidth=6cm]{geometry}



\usepackage{xparse}
\usepackage[unique=true]{bashful}
\usepackage{marginnote}
\usepackage{letltxmacro}
\usepackage{hyperref}

 \LetLtxMacro\hrefnormal\href
 \DeclareDocumentCommand\href{o m m}{%
    \IfNoValueTF{#1}
    {\hrefnormal{#2}{#3}}
    {\hrefnormal[#1]{#2}{#3}}%
    \marginnote{URL: \splice{curl -IsS \detokenize{#2} 2>&1 | head -n 1 |  sed \detokenize{'s/HTTP\/1\.1 ... //'}}}
  }




\begin{document}
\setlength{\parindent}{0pt}
\begin{center}
 {\huge\textbf{URL Validation}}
\end{center}

Hidden URLs:

\href{http://tex.stackexchange.com/test-404/}{This will give a 404 - Page Not Found}

\href{http://tex.stackexchange.com/questions/356377/rename-href-command-to-check-arguments-first}{Page Found}%
\end{document}

在此处输入图片描述

相关内容