
我有一個 tex 文件,其中包含一些我自己的自訂函數,其中之一是 switch case 函數。
因為我使用 \input 從其他文檔呼叫此文件,所以我需要輸出變數是本地的。但是,當我迭代循環的其餘部分時,我需要一個局部變數來儲存輸出。要么中止循環並轉到函數末尾。
我正在使用 texmaker 和 pdftex
\usepackage{xparse}
\usepackage{fp}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{printlen}
\usepackage{listofitems}
\usepackage{forloop}
\usepackage{pgffor}
\NewDocumentCommand\SwitchCase{m m}
{
\setsepchar{,/-}
\readlist\Cases{#2}
\FPadd\CaseCount{\Caseslen}{0}
\def\out{}
\foreach \ct in {1,...,\CaseCount}
{
\ifthenelse{\equal{#1}{\Cases[\ct ,1]}}
{
\def\tmp{\Cases[\ct, 2]}
%\tmp
\def\out{\tmp}
}
{}
}
\out
}
%below is a sample of how this code would work
%not exactly how I would use it, but still valid example
\begin{document}
\SwitchCase{hello}{hel-1,he-2,hello-3,Hello-4}
\end{document}
在給出的範例中,該函數應傳回 3,但是當我嘗試在循環中設定輸出變數時,它會傳回 0 或不傳回任何內容。
這個函數在我的文件中有很多可能的用途,因此它必須是模糊的。
ps 如果有任何措辭尷尬,我深表歉意,我在語法和單字選擇方面不是最好的。
答案1
您可以在不使用此處所有套件的情況下實現該功能:
\documentclass{article}
\makeatletter
\newcommand\SwitchCase[2]{%
\def\tmpa{#1}%
\@for\tmp:=#2\do{\expandafter\zz@switch\tmp\zz@switch}%
}
\def\zz@switch#1-#2\zz@switch{%
\def\tmpb{#1}%
\ifx\tmpa\tmpb#2\fi}
\makeatother
\begin{document}
\SwitchCase{hello}{hel-1,he-2,hello-3,Hello-4}
\end{document}
答案2
這是常見的問題:\foreach
循環是在一組中完成的。
另外,你必須完全展開 的替換文字\tmp
(但你不需要它,可以直接定義\out
)。
\documentclass{article}
\usepackage{xparse}
\usepackage{fp}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{printlen}
\usepackage{listofitems}
\usepackage{forloop}
\usepackage{pgffor}
\NewDocumentCommand\SwitchCase{m m}
{%
\setsepchar{,/-}%
\readlist\Cases{#2}%
\foreach \ct in {1,...,\Caseslen}
{%
\ifthenelse{\equal{#1}{\Cases[\ct ,1]}}
{%
\xdef\out{\Cases[\ct, 2]}
}
{}%
}%
\out
}
\begin{document}
\SwitchCase{hello}{hel-1,he-2,hello-3,Hello-4}
\end{document}
這輸出 3。
請注意未受保護的行尾,它們會產生可能不會被忽略的空格,這取決於呼叫巨集的上下文。
更短的版本使用expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand\SwitchCase{m m}
{
\clist_map_inline:nn { #2 }
{
\__cdickstein_switchcase_item:nn { #1 } { ##1 }
}
}
\cs_new:Nn \__cdickstein_switchcase_item:nn
{
\__cdickstein_switchcase_item:nw { #1 } #2 \q_stop
}
\cs_new:Npn \__cdickstein_switchcase_item:nw #1 #2 - #3 \q_stop
{
\str_if_eq:nnT { #1 } { #2 } { \clist_map_break:n { #3 } }
}
\ExplSyntaxOff
\begin{document}
\SwitchCase{hello}{hel-1,he-2,hello-3,Hello-4}
\end{document}
這在第一場比賽時停止。
一個可擴展的版本,再次在第一場比賽時停止。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand\SwitchCase{m m}
{
\__cdickstein_switchcase:nw { #1 } #2 , , \q_nil
}
\cs_new:Npn \__cdickstein_switchcase:nw #1 #2 ,
{
\tl_if_blank:nTF { #2 }
{
\use_none:n
}
{
\__cdickstein_switchcase_item:nw { #1 } #2 \q_stop
\__cdickstein_switchcase:nw { #1 }
}
}
\cs_new:Npn \__cdickstein_switchcase_item:nw #1 #2 - #3 \q_stop
{
\str_if_eq:nnT { #1 } { #2 } { #3 \__cdickstein_swithcase_break:w }
}
\cs_new:Npn \__cdickstein_swithcase_break:w #1 \q_nil {}
\ExplSyntaxOff
\begin{document}
\SwitchCase{hello}{hel-1,he-2,hello-3,Hello-4}
\SwitchCase{x}{x-1,y-2,x-3}
\end{document}