我定义了在我的文档中使用不同类型的图片(人类、动物、形状等)。
有没有办法为这些不同类型设置计数器,以便我可以追踪不同类别有多少张图片!
\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz}
\begin{document}
\def\arrowa
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\humana
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\humanb
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\housea
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\boata
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\boatb
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\birda
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\birdb
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\turtlea
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\candlea
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\candleb
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\horseb
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\doga
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\cata
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\def\rabbita
{%
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
number of human figures: 6
number of dog figures: 4
number of candle figures: 2
number of horse figures: 8
\end{document}
答案1
我建议使用expl3
属性列表(l3prop
)来存储每个类别的图片数量。
注意:\nbPicsInCategoryExp
是的完全可扩展的“变体” :前者理论上比\nbPicsInCategory
后者慢(具有大量类别),但可以在仅扩展的上下文中使用:在、、、和宏内部\numexpr
,以及很多其他地方。\dimexpr
\edef
siunitx
\num
\SI
\documentclass{article}
\usepackage{xparse} % only necessary if your LaTeX is older than 2020-10-01
\usepackage{tikz}
\ExplSyntaxOn
\prop_new:N \g_hany_picture_counts_prop
% #1: macro name for the picture, without the initial backslash
% #2: category name
\cs_new_protected:Npn \hany_define_picture:nn #1#2
{
% Get current counter value for the category in \l_tmpa_tl, or 0 if no
% picture has been put in this category yet.
\prop_get:NnNF \g_hany_picture_counts_prop {#2} \l_tmpa_tl
{ \tl_set:Nn \l_tmpa_tl { 0 } }
% Increase the counter for this category.
\prop_gput:Nnx \g_hany_picture_counts_prop {#2}
{ \int_eval:n { \l_tmpa_tl + 1 } }
% Define the macro containing the picture
\cs_new_protected:cpn {#1}
}
% Same arguments as for \hany_define_picture:nn
\NewDocumentCommand \myDefinePicture { m m }
{
\hany_define_picture:nn {#1} {#2}
}
% #1: category name
\NewDocumentCommand \nbPicsInCategory { m }
{
\prop_get:NnNF \g_hany_picture_counts_prop {#1} \l_tmpa_tl
{ \tl_set:Nn \l_tmpa_tl { 0 } }
\tl_use:N \l_tmpa_tl
}
\cs_new:Npn \__hany_nb_pics_in_category:n #1
{ \tl_if_empty:nTF {#1} { 0 } {#1} }
\cs_generate_variant:Nn \__hany_nb_pics_in_category:n { f }
% Expandable version of \nbPicsInCategory. This allows using the result in
% expansion-only contexts, which is an advantage over \nbPicsInCategory,
% however \nbPicsInCategoryExp is slower (which is only noticeable with a
% large enough number of categories!).
\NewExpandableDocumentCommand \nbPicsInCategoryExp { m }
{
\__hany_nb_pics_in_category:f
{ \prop_item:Nn \g_hany_picture_counts_prop {#1} }
}
\NewExpandableDocumentCommand \nbCategories { }
{
\prop_count:N \g_hany_picture_counts_prop
}
\cs_new:Npn \__hany_count_picture:nn #1#2 { + #2 }
\NewExpandableDocumentCommand \nbPictures { }
{
\int_eval:n
{
0
\prop_map_tokens:Nn \g_hany_picture_counts_prop
{ \__hany_count_picture:nn }
}
}
\ExplSyntaxOff
\begin{document}
\myDefinePicture{arrowa}{arrows}{%
\begin{tikzpicture}
\draw[->] (0,0) -- (1,1);
\end{tikzpicture}%
}
\myDefinePicture{humana}{humans}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{humanb}{humans}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{housea}{houses}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{boata}{boats}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{boatb}{boats}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{birda}{birds}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{birdb}{birds}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{turtlea}{turtles}{%
\begin{tikzpicture}
\fill[green!20!brown] (0,0) ellipse[x radius=1cm, y radius=1.2cm];
\end{tikzpicture}%
}
\myDefinePicture{candlea}{candles}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{candleb}{candles}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{doga}{dogs}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{dogb}{dogs}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\noindent
Number of categories: \nbCategories
\qquad \arrowa \qquad \turtlea\\
Number of pictures: \nbPictures
\noindent
Number of birds, dogs and turtles:
$\nbPicsInCategory{birds} + \nbPicsInCategory{dogs} + \nbPicsInCategory{turtles}
= \the\numexpr \nbPicsInCategoryExp{birds} +
\nbPicsInCategoryExp{dogs} +
\nbPicsInCategoryExp{turtles} \relax$.
\medskip \noindent
Number of arrows: \nbPicsInCategory{arrows}\\
Number of human figures: \nbPicsInCategory{humans}\\
Number of bird figures: \nbPicsInCategory{birds}\\
Number of dog figures: \nbPicsInCategory{dogs}\\
Number of candle figures: \nbPicsInCategory{candles}\\
Number of house figures: \nbPicsInCategory{houses}\\
Number of turtle figures: \nbPicsInCategory{turtles}\\
Number of boat figures: \nbPicsInCategory{boats}
\end{document}
答案2
像这样?
\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz}
\newcounter{doga} % < -- one counter for each kind of figure
\def\doga
{%
\stepcounter{doga}% % <-- add 1 to counter only when using the \doga command
\begin{tikzpicture}[]%
\end{tikzpicture}
}%
\begin{document}
number of dog figures: \thedoga\
\doga % you have to use at least once to increase counter
number of dog figures: \thedoga\
\doga % everytime you use, add 1 to counter
number of dog figures: \thedoga\
\end{document}
答案3
这不是对所提问题的回答,而是对评论的回复。这实际上应该是对另一个问题的回答。
文档末尾处有数字,但如果使用两次运行,则可以在文档开头打印数字。
使用tocloft
包,您可以定义一个新列表(例如mynums
)和相关存储文件(例如.nol
)。在文档末尾,当所有数字已知且最终确定后,.nol
使用将它们写入文件\addtocontents{nol}{...}
。
然后,您可以打印以 开头的文档中的列表\listofmynums
。需要运行两次编译。
为了说明,该示例使用@frougon 的 expl3 答案作为基础,但类似的方法适用于任何答案:
我将打印命令更改为\nbPicsInCategoryExp
版本,以便扩展值进入.nol
文件而不是命令名称。
平均能量损失
\documentclass{article}
\usepackage{tocloft}
\newlistof{mynums}{nol}{Summary}
\usepackage{etoolbox}
\usepackage{xparse} % only necessary if your LaTeX is older than 2020-10-01
\usepackage{tikz}
\ExplSyntaxOn
\prop_new:N \g_hany_picture_counts_prop
% #1: macro name for the picture, without the initial backslash
% #2: category name
\cs_new_protected:Npn \hany_define_picture:nn #1#2
{
% Get current counter value for the category in \l_tmpa_tl, or 0 if no
% picture has been put in this category yet.
\prop_get:NnNF \g_hany_picture_counts_prop {#2} \l_tmpa_tl
{ \tl_set:Nn \l_tmpa_tl { 0 } }
% Increase the counter for this category.
\prop_gput:Nnx \g_hany_picture_counts_prop {#2}
{ \int_eval:n { \l_tmpa_tl + 1 } }
% Define the macro containing the picture
\cs_new_protected:cpn {#1}
}
% Same arguments as for \hany_define_picture:nn
\NewDocumentCommand \myDefinePicture { m m }
{
\hany_define_picture:nn {#1} {#2}
}
% #1: category name
\NewDocumentCommand \nbPicsInCategory { m }
{
\prop_get:NnNF \g_hany_picture_counts_prop {#1} \l_tmpa_tl
{ \tl_set:Nn \l_tmpa_tl { 0 } }
\tl_use:N \l_tmpa_tl
}
\cs_new:Npn \__hany_nb_pics_in_category:n #1
{ \tl_if_empty:nTF {#1} { 0 } {#1} }
\cs_generate_variant:Nn \__hany_nb_pics_in_category:n { f }
% Expandable version of \nbPicsInCategory. This allows using the result in
% expansion-only contexts, which is an advantage over \nbPicsInCategory,
% however \nbPicsInCategoryExp is slower (which is only noticeable with a
% large enough number of categories!).
\NewExpandableDocumentCommand \nbPicsInCategoryExp { m }
{
\__hany_nb_pics_in_category:f
{ \prop_item:Nn \g_hany_picture_counts_prop {#1} }
}
\NewExpandableDocumentCommand \nbCategories { }
{
\prop_count:N \g_hany_picture_counts_prop
}
\cs_new:Npn \__hany_count_picture:nn #1#2 { + #2 }
\NewExpandableDocumentCommand \nbPictures { }
{
\int_eval:n
{
0
\prop_map_tokens:Nn \g_hany_picture_counts_prop
{ \__hany_count_picture:nn }
}
}
\ExplSyntaxOff
\begin{document}
\listofmynums
Material in document \ldots
\myDefinePicture{arrowa}{arrows}{%
\begin{tikzpicture}
\draw[->] (0,0) -- (1,1);
\end{tikzpicture}%
}
\myDefinePicture{humana}{humans}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{humanb}{humans}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{housea}{houses}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{boata}{boats}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{boatb}{boats}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{birda}{birds}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{birdb}{birds}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{turtlea}{turtles}{%
\begin{tikzpicture}
\fill[green!20!brown] (0,0) ellipse[x radius=1cm, y radius=1.2cm];
\end{tikzpicture}%
}
\myDefinePicture{candlea}{candles}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{candleb}{candles}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{doga}{dogs}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
\myDefinePicture{dogb}{dogs}{%
\begin{tikzpicture}
\end{tikzpicture}%
}
%Summary:
\addtocontents{nol}{Number of categories: \nbCategories\par}
\addtocontents{nol}{Number of pictures: \nbPictures\par}
\addtocontents{nol}{Number of birds, dogs and turtles:
$\nbPicsInCategoryExp{birds} + \nbPicsInCategoryExp{dogs} + \nbPicsInCategoryExp{turtles}
= \the\numexpr \nbPicsInCategoryExp{birds} +
\nbPicsInCategoryExp{dogs} +
\nbPicsInCategoryExp{turtles} \relax$.\par
}
\addtocontents{nol}{%
Number of arrows: \nbPicsInCategoryExp{arrows}\par
Number of human figures: \nbPicsInCategoryExp{humans}\par
Number of bird figures: \nbPicsInCategoryExp{birds}\par
Number of dog figures: \nbPicsInCategoryExp{dogs}\par
Number of candle figures: \nbPicsInCategoryExp{candles}\par
Number of house figures: \nbPicsInCategoryExp{houses}\par
Number of turtle figures: \nbPicsInCategoryExp{turtles}\par
Number of boat figures: \nbPicsInCategoryExp{boats}\par
\smallskip
\hrule\par\bigskip}
\vdots
\ldots End of document
\end{document}