将宏传递给需要可选星号的函数

将宏传递给需要可选星号的函数

最初,这是为了使用条件排版,xparse而不是其他建议的包这里。也许有比我所追求的方法更好的方法,但无论如何,这个问题应该与xparse。在“换句话说”下应该写着“如果您正在从移动设备上阅读,纸张大小应该看起来合适。”。

\documentclass{scrreprt}
%\usepackage{lipsum}
\usepackage{xparse}

% Uncomment either of the two <---------
\NewDocumentCommand{\ismobile}{}{*}
%\NewDocumentCommand{\ismobile}{}{}
% ------------------------------------->
\NewDocumentCommand{\ifMobile}{smm}
{
  \IfBooleanTF{#1}
  {#2}
  {#3}  
}\NewDocumentCommand{\ifmobile}{mm}
{
  \ifMobile\ismobile{#1}{#2}
}

\ifmobile
{
  \KOMAoption{paper}{A6}
  \KOMAoption{DIV}{20}
}
{
  \KOMAoption{paper}{A4}
}

\begin{document}

 If you are reading from a%TODO suppress gap here
 \ifMobile*{mobile device}{PC}%TODO suppress gap here
 , paper size should be \ifMobile*{A6}{A4}. 

And:

 If you are reading from a%TODO suppress gap here
 \ifMobile{mobile device}{PC}%TODO suppress gap here
 , paper size should be \ifMobile{A6}{A4}. 

In otherwords:

 If you are reading from a
 \ifmobile{mobile device}{PC}%BUG
 , paper size should look appropriate.

%\lipsum[1]
\end{document}

射击

答案1

正如 egreg 所说,你的版本不起作用,因为xparse寻找一个明确的 *。这是一个使用布尔变量的版本expl3

我定义了一个布尔变量\g__erwann_mobile_bool,在移动版本中应为 true,否则为 false。命令\MobileVersion\PrintVersion相应地切换该布尔值。

该命令\ifmobileTF检查该布尔值并选择正确的版本(我更改了名称,因为我花了一段时间才意识到你有\ifMobile:-) \ifmobile

如果给出了可选的星号,则该命令\ifMobile将选择第一个参数,否则将选择第二个参数。不过,我必须说,我看不出这个命令有什么用处……

代码如下:

\documentclass{scrreprt}
\usepackage{xparse}

\ExplSyntaxOn
\bool_new:N \g__erwann_mobile_bool
\NewDocumentCommand \MobileVersion { }
  { \bool_gset_true:N \g__erwann_mobile_bool }
\NewDocumentCommand \PrintVersion { }
  { \bool_gset_false:N \g__erwann_mobile_bool }
\NewDocumentCommand \ifmobileTF { +m +m }
  {
    \bool_if:NTF \g__erwann_mobile_bool
      {#1} {#2}
  }
\NewDocumentCommand \ifMobile { s +m +m }
  {
    \IfBooleanTF {#1}
      {#2} {#3}
  }
\ExplSyntaxOff
% Allow these only in the preamble
\makeatletter
\@onlypreamble \MobileVersion
\@onlypreamble \PrintVersion
\makeatother

% Set appropriate version
\MobileVersion
% \PrintVersion

\ifmobileTF
  {
    \KOMAoption{paper}{A6}
    \KOMAoption{DIV}{20}
  }
  {
    \KOMAoption{paper}{A4}
  }

\begin{document}

 If you are reading from a
 \ifMobile*{mobile device}{PC},
 paper size should be \ifMobile*{A6}{A4}.

And:

 If you are reading from a
 \ifMobile{mobile device}{PC},
 paper size should be \ifMobile{A6}{A4}.

In otherwords:

 If you are reading from a
 \ifmobileTF{mobile device}{PC},
 paper size should look appropriate.

\end{document}

答案2

使用xparse并不能免除对行尾的保护。

您的问题是\ismobile不是*,而是最终打印 的东西*。另一方面,带有s参数的命令会测试是否存在显式*

可以确实喜欢

\documentclass{scrreprt}
\usepackage{xparse}

% Uncomment either of the two <---------
%\newcommand{\ismobile}{*}
\newcommand{\ismobile}{}

% ------------------------------------->
\NewDocumentCommand{\ifMobile}{smm}
{%
  \IfBooleanTF{#1}{#2}{#3}%
}
\NewDocumentCommand{\ifmobile}{mm}
{%
  \expandafter\ifMobile\ismobile{#1}{#2}%
}

\ifmobile
{
  \KOMAoption{paper}{A6}
  \KOMAoption{DIV}{20}
}
{
  \KOMAoption{paper}{A4}
}

\begin{document}

If you are reading from a \ifmobile{mobile device}{PC},
paper size should look appropriate.

\end{document}

更轻松:

\documentclass{scrreprt}
\usepackage{etoolbox}

\newtoggle{mobile}
\toggletrue{mobile}
\newcommand{\ifmobile}[2]{\iftoggle{mobile}{#1}{#2}}

\ifmobile
{
  \KOMAoption{paper}{A6}
  \KOMAoption{DIV}{20}
}
{
  \KOMAoption{paper}{A4}
}

\begin{document}

If you are reading from a \ifmobile{mobile device}{PC},
paper size should look appropriate.

\end{document}

注释掉\toggletrueA4 纸的行。

答案3

这是我通过反复试验得出的结论(需要重新熟悉expl3),所以我认为\exp_last_two_unbraced:Noo它不是最直接的 FW。

\documentclass{scrreprt}
%\usepackage{lipsum}
\usepackage{expl3}
\usepackage{xparse}

%% Uncomment either of the two <---------
%\NewDocumentCommand{\ismobile}{}{*}
%%\NewDocumentCommand{\ismobile}{}{}
%% ------------------------------------->

\NewDocumentCommand{\ifMobile}{smm}
{
  \IfBooleanTF{#1}
  {#2}
  {#3}
}

\ExplSyntaxOn   
\tl_set:Nn \l_ismobile_tl {*}
% \tl_set:Nn \l_ismobile_tl {}
\ExplSyntaxOff

\ExplSyntaxOn   %Why outer?
\NewDocumentCommand{\ifmobile}{mm}
{
%   \exp_after:wN \ifMobile \exp_after:wN % Nope
%   \exp_args:NNo \ifMobile % Nope
%  \l_ismobile_tl
%  {#1}{#2}
  \exp_last_two_unbraced:Noo\ifMobile
  {\l_ismobile_tl}{{#1}{#2}}
}
  \ExplSyntaxOff

\ifmobile
{
  \KOMAoption{paper}{A6}
  \KOMAoption{DIV}{20}
}
{
  \KOMAoption{paper}{A4}
}

\begin{document}

 If you are reading from a%TODO suppress gap here
 \ifMobile*{mobile device}{PC}%TODO suppress gap here
 , paper size should be \ifMobile*{A6}{A4}. 

And:

 If you are reading from a%TODO suppress gap here
 \ifMobile{mobile device}{PC}%TODO suppress gap here
 , paper size should be \ifMobile{A6}{A4}. 

In otherwords:

 If you are reading from a
 \ifmobile{mobile device}{PC}%BUG
 , paper size should look appropriate.

%\lipsum[1]
\end{document}

A6

A4

相关内容