xstring字體改變

xstring字體改變

簡單的問題。我有兩個巨集用於顯示一周中的縮寫天數,例如 MWF 或 TR。我不確定為什麼 MWE 中的第一個巨集失敗而第二個巨集有效。我更喜歡像第一個宏那樣的東西,因為它更短。

\documentclass{article}
\usepackage{stix2}
\usepackage{suffix}
\WithSuffix\newcommand\textsc*[1]{%
    \textsc{\MakeLowercase{#1}}%
}
\usepackage{tikz}
\newcommand{\Repeat}[2]{%\repeat already used
    \foreach \n in {1,...,#1}{#2}%
}
\usepackage{xstring}
%\newcommand{\weekdays}[1]{%doesn't work
%   \textsc*{%
%       \IfSubStr{#1}{1}{N}{}%Sunday
%       \IfSubStr{#1}{2}{M}{}%Monday
%       \IfSubStr{#1}{3}{T}{}%Tuesday
%       \IfSubStr{#1}{4}{W}{}%Wednesday
%       \IfSubStr{#1}{5}{R}{}%Thursday
%       \IfSubStr{#1}{6}{F}{}%Friday
%       \IfSubStr{#1}{7}{S}{}%Saturday
%   }%
%}
\newcommand{\weekdays}[1]{%
    \IfSubStr{#1}{1}{\textsc*{N}}{}%Sunday
    \IfSubStr{#1}{2}{\textsc*{M}}{}%Monday
    \IfSubStr{#1}{3}{\textsc*{T}}{}%Tuesday
    \IfSubStr{#1}{4}{\textsc*{W}}{}%Wednesday
    \IfSubStr{#1}{5}{\textsc*{R}}{}%Thursday
    \IfSubStr{#1}{6}{\textsc*{F}}{}%Friday
    \IfSubStr{#1}{7}{\textsc*{S}}{}%Saturday
}
\begin{document}
\weekdays{246}\quad\weekdays{35}
\Repeat{7}{\par\weekdays{\n}}
\end{document}

答案1

我不知道為什麼\MakeLowercase當你可以在定義中使用小寫時讓事情變得複雜。

無論如何,xstring命令不可擴展,這就是您遇到的問題。

它可能可以解決xstring,但有更好的方法。

\documentclass{article}
\usepackage{stix2}
\usepackage{tikz}
\newcommand{\Repeat}[2]{%\repeat already used
    \foreach \n in {1,...,#1}{#2}%
}

\ExplSyntaxOn

\NewDocumentCommand{\textsmallcaps}{m}
 {
  \textsc { \text_lowercase:n { #1 } }
 }

\NewExpandableDocumentCommand{\weekdayinitial}{m}
 {
  \exp_args:Ne \str_map_function:nN { #1 } \egreg_weekday:n
 }

\cs_new:Nn \egreg_weekday:n
 {
  \str_case:nn { #1 }
   {
    {1}{N}
    {2}{M}
    {3}{T}
    {4}{W}
    {5}{R}
    {6}{F}
    {7}{S}
   }
 }

\NewDocumentCommand{\weekdays}{m}
 {
  \textsmallcaps { \weekdayinitial { #1 } }
 }

\ExplSyntaxOff

\begin{document}

\weekdays{246}\quad\weekdays{35}

\Repeat{7}{\par\weekdays{\n}}

\end{document}

我不會用suffix

在此輸入影像描述

答案2

with內部透過while\textsc* \MakeLowercase進行完全擴展,不可擴展。 您可以用於防止擴充。\protected@edef\IfSubStr
\unexpanded

\documentclass{article}
\usepackage{stix2}
\usepackage{suffix}
\WithSuffix\newcommand\textsc*[1]{%
    \textsc{\MakeLowercase{#1}}%
}
\usepackage{tikz}
\newcommand{\Repeat}[2]{%\repeat already used
    \foreach \n in {1,...,#1}{#2}%
}
\usepackage{xstring}
\DeclareRobustCommand{\weekdays}[1]{%
   \textsc*{%
     \unexpanded{%
       \IfSubStr{#1}{1}{N}{}%Sunday
       \IfSubStr{#1}{2}{M}{}%Monday
       \IfSubStr{#1}{3}{T}{}%Tuesday
       \IfSubStr{#1}{4}{W}{}%Wednesday
       \IfSubStr{#1}{5}{R}{}%Thursday
       \IfSubStr{#1}{6}{F}{}%Friday
       \IfSubStr{#1}{7}{S}{}%Saturday
     }%
   }%
}
\begin{document}
\weekdays{246}\quad\weekdays{35}
\Repeat{7}{\par\weekdays{\n}}
\end{document}

在此輸入影像描述

透過這種方式,擴展參數會被延遲到小寫之後。完成小寫後,將\IfSubStr執行 - 指令。\IfSubStr也充分展開了論證。
但是,對於小寫的參數,其擴展在小寫期間被阻止,傳遞\csname..\endcsname在某個階段擴展產生數字序列的 - 表達式可能會出現問題,因為小寫可能會改變 和 之間的內容\csname,從而可能會改變從-表達式\endcsname形成的控制序列標記\csname...\endcsname

所以egreg的解決方案是更喜歡。


或者,您可以僅用於\noexpand防止令牌擴充\IfSubStr

\documentclass{article}
\usepackage{stix2}
\usepackage{suffix}
\WithSuffix\newcommand\textsc*[1]{%
    \textsc{\MakeLowercase{#1}}%
}
\usepackage{tikz}
\newcommand{\Repeat}[2]{%\repeat already used
    \foreach \n in {1,...,#1}{#2}%
}
\usepackage{xstring}
\DeclareRobustCommand{\weekdays}[1]{%
   \textsc*{%
       \noexpand\IfSubStr{#1}{1}{N}{}%Sunday
       \noexpand\IfSubStr{#1}{2}{M}{}%Monday
       \noexpand\IfSubStr{#1}{3}{T}{}%Tuesday
       \noexpand\IfSubStr{#1}{4}{W}{}%Wednesday
       \noexpand\IfSubStr{#1}{5}{R}{}%Thursday
       \noexpand\IfSubStr{#1}{6}{F}{}%Friday
       \noexpand\IfSubStr{#1}{7}{S}{}%Saturday
   }%
}
\begin{document}
\weekdays{246}\quad\weekdays{35}
\Repeat{7}{\par\weekdays{\n}}
\par
\def\foobar{1357}
\weekdays{\foobar}
\end{document}

在此輸入影像描述

仍然不會阻止參數的小寫,使用自訂的\lccode設定可能會導致意外的結果。

相關內容