從小寫字串建立並使用大寫 CS

從小寫字串建立並使用大寫 CS

我正在尋找一種方法來完成以下任務

我想要一個命令

\setcitation[2]

具有以下定義

\def\setcitation#1#2{\lowercase{\expandafter\gdef\csname mycommoncitation#1\endcsname}{#2}}

本質上,它的作用是每次使用參數 xXx 和 yyy 呼叫它時,它都會建立一個新命令

\mycommoncitationxxx

定義為

\yyy

現在,給定輸入 xxX,我想輸出 yyy。那麼會發生什麼:將 xxX 轉換為小寫 xxx,呼叫 \mycommonitationxxx

我嘗試這樣做:

\newcommand\getcitation[1]{%
\lowercase{\csuse{mycommoncitation#1}}
}

如下:

\newcommand\getcitation[1]{\lowercase{\expandafter\gdef\csname mycommoncitation#1\endcsname}}

但不管用。

對於第一部分(我的設定命令的定義),我的解決方案是基於從小寫字串建立大寫的 CS 並給它一個定義

對於第二部分,我還沒有找到任何幫助。

完整的範例,也顯示了我想如何使用這些命令:

\documentclass{article} 
\usepackage{etoolbox} 
\newcommand{\setcitation}[2]{\lowercase{\csdef{mycommoncitation#1}}{#2}} 
\newcommand{\getcitation}[1]{\lowercase{\csuse{mycommoncitation#1}}} 

\newcommand{\getcitationifexistsotherwiseinput}[1]{\lowercase{\ifcsdef{mycommoncitation#1}{\getcitation{#1}}{#1}}}


\begin{document} 
\setcitation{Foo}{Bar} 
\getcitation{Foo} 
\mycommoncitationfoo
\getcitationifexistsotherwiseinput{Foo} %My expectation: Bar
\getcitationifexistsotherwiseinput{foo} %my expectation: Bar
\getcitationifexistsotherwiseinput{boo} %my expectation: boo
%Now I want a command that returns 
% \getcitation{FOO} 

\cite{\getcitation{foo}} %my expectaiton: "undefined citation Bar"
\end{document}

在這裡,在 \cite{...} 行中,事情開始出錯。

答案1

您需要一個可擴展版本\lowercase

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\setcitation}{mm}
 {
  \prop_gput:Nxn \g_bartbog_citations_prop { \str_lowercase:n { #1 } } { #2 }
 }
\DeclareExpandableDocumentCommand{\getcitation}{m}
 {
  \prop_item:Nf \g_bartbog_citations_prop { \str_lowercase:n { #1 } }
 }

\cs_generate_variant:Nn \prop_gput:Nnn { Nx }
\cs_generate_variant:Nn \prop_item:Nn { Nf }
\prop_new:N \g_bartbog_citations_prop
\ExplSyntaxOff

\setcitation{Foo}{Bar} 

\begin{document}
\getcitation{Foo} 
\getcitation{foo} 
\getcitation{FOO} 

\cite{\getcitation{Foo}},
\cite{\getcitation{foo}},
\cite{\getcitation{FOO}}

\begin{thebibliography}{1}

\bibitem{Bar} Whatever

\end{thebibliography}

\end{document}

為了緊湊性,我使用了屬性清單。

在此輸入影像描述

答案2

我剛剛發布了另一個答案可擴展地更改字母大小寫並在 \csname 內使用,無需包

該答案可以在以下位置找到:https://tex.stackexchange.com/a/349895/118714

該答案包含一個例程\UD@ExpandableLowercase,該例程在兩個擴展步驟中提供與拉丁字母表的 26 個字母相對應的 26 個 catcode-11(字母)字元標記的小寫。該例程不需要 eTeX 或類似的擴展。

(!!!請注意,讓 TeX 讀取並標記由拉丁字母表字元組成的輸入通常會產生 catcode-11(字母)字元標記,而擴展\string\meaning\jobmane原語會產生 catcode-12(其他)字元標記! !

作為(可能不喜歡的)副作用,例程確實將匹配的catcode-1-character-tokens 和catcode-2-character-tokens 替換為匹配的curly catcode-1-brace-tokens 和curly catcode-2-brace-對。{}

我認為這對普通 TeX 和 LaTeX2e 來說不應該是問題,因為這些格式通常唯一具有類別代碼 1 的字元是左大括號{,唯一具有類別代碼 2 的字元是右大括號}

相關內容