我需要一个像这样的输出
\cmd
--> “S0” (输出)
\cmd[]
--> “S0” (输出)
\cmd[map]
----> “S1 S2 S3” (输出)
\cmd[map=true]
----> “S1 S2 S3” (输出)
\cmd[map=false]
----> “S0”(输出)
我看了这里和这里. 如何获取命令使用 pgfkey为了那个原因?
\documentclass[a4paper]{article}
\usepackage{tikz}
\pgfkeys{/tikz/.cd,
/map/.is family,
/map/.cd, %<-added /.cd
map/.is choice,
map/.default=false,
map/true/.code={S1 S2 S3},
map/false/.code={S0},
}
\newcommand{\cmd}[1][]{%
%\tikzset{#1}%
\pgfkeys{map=true}
}
\begin{document}
Test:
\cmd[map]
\cmd[]
\end{document}
答案1
代码中的注释...
\documentclass[a4paper]{article}
\usepackage{pgfkeys} %no need to full-load tikz
\pgfkeys{%/tikz/.cd, you want your top-family, no?
% /map/.is family,
% /map/.cd, %<-added .cd why?
/map/.is choice,
% /map/.default=false, % not useful in choices, you have to call one of them anyway
/map/true/.code={S1 S2 S3},
/map/false/.code={S0},
}
\newcommand{\cmd}[1][false]{%
\pgfkeys{/map=#1}
}
\begin{document}
Test:
\cmd[true]
\cmd % if you use \cmd[] you ends with /map= which is not a choice
\end{document}
请注意,true
和false
在这里是完全可定制的 --- 您可以随意称呼它们,它们只是名称而已。
无论如何,我宁愿将密钥管理和代码分开。所以我会
- 为我的应用程序定义顶级系列
- 添加特定功能来设置此系列中的键
- 使用按键来切换/设置
像这样:
\documentclass[a4paper]{article}
\usepackage{pgfkeys}
\newif\ifdomap\domapfalse
\pgfkeys{
/cis/.is family, /cis/.cd,
map/.is choice,
map/true/.code={\domaptrue},
map/false/.code={\domapfalse},
}
\newcommand{\cisset}[1]{\pgfkeys{/cis/.cd, #1}}
\newcommand{\cmd}[1][]{%
\begingroup % change the keys locally
\cisset{#1}%
\ifdomap
S1 S2 S3
\else
S0
\fi
\endgroup
}
\begin{document}
Test:
\par No map: \cmd
\par Map (locally): \cmd[map=true]
\par No map: \cmd
\par Set globally to true \cisset{map=true}
\par Map: \cmd
\par No map (locally): \cmd[map=false]
\par Set globally to false \cisset{map=false}
\par No map: \cmd
\end{document}
这有点低级,xifthen
如果您只处理 LaTeX,则可以使用包。
如果您想要一个简单的 map
密钥,那么就不要使用is choice
处理程序:
\documentclass[a4paper]{article}
\usepackage{pgfkeys}
\newif\ifdomap
\pgfkeys{
/cis/.is family, /cis/.cd,
map/.code={\domaptrue},
no map/.code={\domapfalse},
no map % set the default
}
\newcommand{\cisset}[1]{\pgfkeys{/cis/.cd, #1}}
\newcommand{\cmd}[1][]{%
\begingroup % change the keys locally
\cisset{#1}%
\ifdomap
S1 S2 S3
\else
S0
\fi
\endgroup
}
\begin{document}
Test:
\par No map: \cmd
\par Map (locally): \cmd[map]
\par No map: \cmd
\par Set globally to true \cisset{map}
\par Map: \cmd
\par No map (locally): \cmd[no map]
\par Set globally to false \cisset{no map}
\par No map: \cmd
\end{document}
最后,这允许同时拥有map
开关和map=...
开关,类似于 Ti 中的许多选项钾Z. 这使用了一个辅助键和一个带有(可选,感谢.default
)参数的样式。
\documentclass[a4paper]{article}
\usepackage{pgfkeys}
\newif\ifdomap
\pgfkeys{
/cis/.is family, /cis/.cd,
domap/.is choice,
domap/false/.code={\domapfalse},
domap/true/.code={\domaptrue},
map/.style={domap=#1},
map/.default=true, % for this key
domap=false, % global default
}
\newcommand{\cisset}[1]{\pgfkeys{/cis/.cd, #1}}
\newcommand{\cmd}[1][]{%
\begingroup % change the keys locally
\cisset{#1}%
\ifdomap
S1 S2 S3
\else
S0
\fi
\endgroup
}
\begin{document}
Test:
\par No map: \cmd
\par Map (locally): \cmd[map]
\par Map (locally): \cmd[map=true]
\par No map (locally, forced): \cmd[map=false]
\par No map (default): \cmd
\par Set globally to true \cisset{map} % or \cisset{map=true}
\par Map: \cmd
\par No map (locally): \cmd[map=false]
\par Set globally to false \cisset{map=false}
\par No map: \cmd
\end{document}