
私がこの質問をする最初の人だとは信じられませんが、:
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
s により、実際には非常に単純なコマンドであるはずのものがわかりにくくなっています。
答え1
の内部は\ExplSyntaxOn
、:
他の文字と同様に、何かを出力するために直接使用できます。ただし、カテゴリ 12 である必要はなく、カテゴリ 11 でも問題ありません。マクロ名に「触れない」ようにする必要があるだけなので、 は使用できます\c_app_proto_str :
が、 は使用できません\c_app_proto_str:
。
また、変数str
とtl
\..._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 / } }
の最初の引数には\href
カテゴリ 12 のコロンが必要なので、\c_colon_str
そこに を使用する必要があることを指摘してくれた Ulrike Fischer に感謝します。
答え2
主な問題は、プロトコルを残りの部分から分離して、PDF ファイルに正しいリンクを書き込むために、カテゴリ コード 12 の\href
明示的な文字トークンを確認する必要があることです。:
3つのステップで実行できます。
\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}