使用 ExplSyntax 時如何設定冒號

使用 ExplSyntax 時如何設定冒號

我不敢相信我是第一個問這個問題的人,但是有沒有:比在 Expl 語法中設定文字冒號 ( )更好的方法呢\str_use:N\c_colon_str?對於編寫 URL 之類的事情來說,這是相當笨拙的。例如,我正在編寫一個命令來列印範例伺服器上某個頁面的鏈接,現在定義如下:

\NewDocumentCommand \server { m }
{ \href { \str_use:N \c_app_proto_str \str_use:N \c_colon_str // \str_use:N \c_app_host_str / #1 / }
        { \str_use:N \c_app_proto_str \str_use:N \c_colon_str // \linebreak[2]
          \str_use:N \c_app_host_str / \linebreak[2] #1 / } }

所有這些\str_use:N都掩蓋了本來應該是一個非常簡單的命令。

答案1

\ExplSyntaxOn裡面的可以直接用來輸出一些東西,就像任何其他字母一樣,只要:你不是真的需要它有類別12和類別11也可以。\c_app_proto_str :不能使用\c_app_proto_str:.

另外,對於strtl變量,\..._use:N訪問器並不是嚴格必需的,您可以省略它。

因此,您的程式碼片段可以縮短為

\NewDocumentCommand \server { m }
{ \href { \c_app_proto_str \c_colon_str // \c_app_host_str / #1 / }
        { \c_app_proto_str :// \linebreak[2]
          \c_app_host_str / \linebreak[2] #1 / } }

感謝 Ulrike Fischer 指出 的第一個參數\href需要有一個 12 類冒號,因此我們需要\c_colon_str在那裡使用。

答案2

主要問題是\href想要查看類別代碼 12 的顯式字元標記:,以便在 PDF 文件中寫入正確的鏈接,從而將協議與其餘協議隔離。

您可以分三步驟完成。

\documentclass{article}
\usepackage{hyperref}

\ExplSyntaxOn

\NewDocumentCommand \server { m }
 {
  \ar_href:VVn \c_app_proto_str \c_app_host_str { #1 }
 }

\cs_new_protected:Nn \ar_href:nnn
 {% we need one more technical step for the colon
  \__ar_href:Vnnn \c_colon_str { #1 } { #2 } { #3 }
 }
\cs_new_protected:Nn \__ar_href:nnnn
 {
  \href { #2 #1 // #3 / #4 / }
        { #2 #1 // \linebreak[2] #3 / \linebreak[2] #4 / }
 }
\cs_generate_variant:Nn \__ar_href:nnnn { V }
\cs_generate_variant:Nn \ar_href:nnn { VV }

\str_const:Nn \c_app_proto_str { https }
\str_const:Nn \c_app_host_str { example.com }

\ExplSyntaxOff

\begin{document}

\server{whatever}

\end{document}

在此輸入影像描述

我不確定你想要尾隨/.

我在做什麼?我定義了一個通用\ar_href:nnn函數,您也可以在不同的上下文中使用該函數(例如,對於其他協定)及其以字串常數作為參數的變體。

由於冒號的特殊狀態,進一步的步驟更好,因此函數實際上調用另一個函數,其中第一個參數透過變體為冒號提供正確的類別代碼。

如果將冒號添加到常數中會容易得多prot

\documentclass{article}
\usepackage{hyperref}

\ExplSyntaxOn

\NewDocumentCommand \server { m }
 {
  \ar_href:VVn \c_app_proto_str \c_app_host_str { #1 }
 }

\cs_new_protected:Nn \ar_href:nnn
 {
  \href { #1 // #2 / #3 / }
        { #1 // \linebreak[2] #2 / \linebreak[2] #3 / }
 }
\cs_generate_variant:Nn \ar_href:nnn { VV }

\str_const:Nn \c_app_proto_str { https: }
\str_const:Nn \c_app_host_str { example.com }

\ExplSyntaxOff

\begin{document}

\server{whatever}

\end{document}

相關內容