
文字列が常に S1 で始まるようにするために、S2 が S1 で始まっていない場合に別の文字列 S2 の先頭に文字列 S1 を追加する拡張可能なコマンドを記述したいと思います。
現在、xstringパッケージに依存するこのコードがあります
\NewDocumentCommand\forcebeginwith{m m}{%
\edef\expandedstring{#1}%
\edef\expandedbeginning{#2}%
\IfBeginWith{
\expandedstring % String
}{
\expandedbeginning % Beginning
}{
\expandedstring % String
}{
\expandedbeginning\expandedstring % Beginning + String
}
}
例えば :
\forcebeginwith{fancycolor}{fancy} % fancycolor
\forcebeginwith{color}{fancy} % fancycolor
質問:このコマンドを拡張可能なコマンドに変換したいのですNewExpandableDocumentCommand
が、その方法がわかりません (結果のコードが 5 年前のバージョンの LaTeX でも動作するようにしたいので、超最近のコマンド/パッケージは避けたいです)。
答え1
コマンド\forcebeginwith
は で定義されます\NewExpandableDocumentCommand
。テストは で実行されます\str_if_eq:eeTF
。 コマンドは、以下の例のように\forcebeginwith
内で使用できます。\edef
\documentclass[border=6pt,varwidth]{standalone}
\ExplSyntaxOn
\NewExpandableDocumentCommand { \forcebeginwith } { m m }
{
\str_if_eq:eeTF {#2} { \str_range:nnn {#1} { 1 } { \str_count:n {#2} } }
{#1}
{ #2#1 }
}
\ExplSyntaxOff
\begin{document}
\forcebeginwith{fancycolor}{fancy}\\% fancycolor
\forcebeginwith{color}{fancy}\\% fancycolor
\edef\testA{\forcebeginwith{LaTeX}{La}}\testA\\
\edef\testB{\forcebeginwith{TeX}{La}}\testB
\end{document}
答え2
を使用する場合はpdflatex
、次のように実行できます。
\documentclass{article}
\newcommand{\forcebeginwith}[2]{%
\ifnum\pdfmatch{^#2}{#1}=0 #2\fi#1%
}
\begin{document}
\forcebeginwith{fancycolor}{fancy}% fancycolor
\forcebeginwith{color}{fancy}% fancycolor
\edef\testA{\forcebeginwith{LaTeX}{La}}\testA
\edef\testB{\forcebeginwith{TeX}{La}}\testB
\end{document}
TeX Live 2012 までテストしました。
答え3
TeX プリミティブのみを使用する場合、コードは次のようになります。
\def\forcebeginwith#1#2{\fbwA .#1\end .#2\end{#1}{#2}}
\def\fbwA #1#2\end #3#4\end #5#6{%
\ifx #1#3%
\ifx \end#4\end #5% S2 is included at the start of S1, print S1 only
\else
\ifx \end#2\end {#5} shorter than {#6}, something wrong%
\else \fbwB {#2\end #4\end {#5}{#6}}%
\fi
\fi
\else #6#5% S2 isn't inluded at the start of S1, print S2S1.
\fi
}
\def\fbwB #1\fi\fi#2\fi{\fi\fi\fi \fbwA #1}
% test:
\message{\forcebeginwith{fancycolor}{fancy}}
\message{\forcebeginwith{color}{fancy}}
\bye
TeX は「文字列」では動作せず、トークン リストのみであることに注意してください。
答え4
次の LuaLaTeX ベースのソリューションは、 というユーザー マクロを定義します\forcebeginwith
。プリミティブのプロパティは \directlua
最初の登場以来ほとんど変更されていないため、これは 10 年以上前の LaTeX カーネルのバージョンでも動作するはずです。\directlua
は拡張可能なので、 も拡張可能です\forcebeginwith
。 の引数は\forcebeginwith
ASCII エンコードである必要はなく、UTF8 エンコードが可能であることに注意してください。
% !TEX program = lualatex
\documentclass{article} % or some other suitable document class
\directlua{% Define the Lua function 'forcebeginwith':
function forcebeginwith ( s1 , s2 )
if unicode.utf8.sub ( s1 , 1 , unicode.utf8.len ( s2 ) ) == s2 then
return ( s1 )
else
return ( s2..s1 )
end
end
}
\newcommand\forcebeginwith[2]{%
\directlua{ tex.sprint ( forcebeginwith ( "#1" , "#2" ) ) }}
\newcommand\Za{color}
\newcommand\Zb{fancy}
\begin{document}
\obeylines % just for this document
\forcebeginwith{fancycolor}{fancy}
\forcebeginwith{color}{fancy}
\smallskip
% Demonstrate that '\forcebeginwith' is expandable
\forcebeginwith{\Zb\Za}{\Zb}
\forcebeginwith{\Za}{\Zb}
\end{document}