我有以下代码:
\documentclass{article}
\usepackage{ifthen}
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\newcommand{\probName}{}%
\newcounter{probCounter}%
\setcounter{probCounter}{1}
\newcounter{partCounter}%
\makeatletter
\newcommand{\howmany}[2][subsection]{%
\begingroup
\@namedef{the#1}{\arabic{#1}}%
\addtocounter{#1}{\m@ne}%
\refstepcounter{#1}%
\label{#2}%
\endgroup}
\makeatother
\newenvironment{prob}[1][]%
{\setcounter{partCounter}{0}
\renewcommand{\probName}{#1}%
\newcommand{\abc}{\ref{noPart:\arabic{probCounter}}}
\section{{Problem \arabic{probCounter}}{: \probName} (contains
\abc\
%\ifthenelse{\abc=0}{1}{\abc}\ %%%%%%%%%%%%%%%%%%%%%% does not work
parts) }% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wanna conditional parts/part
}{\howmany{noPart:\arabic{probCounter}}
\stepcounter{probCounter}%
}
\newcommand{\partName}{}%
\renewenvironment{part}[1][]%
{\stepcounter{partCounter}%
\renewcommand{\partName}{#1}%
\subsection{{Part \Alph{partCounter}}{: \partName}}
}{}
\newenvironment{ques}[1][]{
\ifthenelse{
\equal{#1}{}
}{}{
\ifthenelse{
\equal{#1}{1}
}{
\textbf{(#1 point)}
}{
\textbf{(#1 points)}
}}
}{}
\begin{document}
\begin{prob}[Prob 1]
\begin{part}
\end{part}
\begin{part}
\end{part}
\begin{part}
\end{part}
\end{prob}
\begin{prob}[Prob 2] %%%%%%%%%%% force to show => (contains 1 part)
\end{prob}
\begin{prob}[Prob 3] %%%%%%%%%%% correct the grammar => (contains 1 part)
\begin{part}
\end{part}
\end{prob}
\begin{prob}[Prob 4]
\begin{part}
\end{part}
\begin{part}
\end{part}
\begin{part}
\end{part}
\begin{part}
\end{part}
\end{prob}
\end{document}
现在我想要的是:
...
问题 2(包含1部分)%% 强制为 1 部分
问题 3(包含1部分)%% 从措辞中删除“s”(部分“s”)
...
我首先想到的是条件 ifthenelse 函数,它也包含在我的代码中,但它没有按预期工作。
答案1
问题是这\ref{...}
不是一个数字,而是一组指令印刷一个号码。
如果引用尚未建立,则可以使用refcount
提供扩展为 0 的包,否则扩展为引用本身。\getrefnumber
我做了一些其他小改动(特别是保护了几个行尾)。对于部分/部分,语法\ifthenelse
很麻烦,我更喜欢直接使用\ifnum
。
\documentclass{article}
\usepackage{ifthen,refcount}
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\newcommand{\probName}{}%
\newcounter{probCounter}%
\setcounter{probCounter}{1}
\newcounter{partCounter}%
\makeatletter
\newcommand{\howmany}[2][subsection]{%
\begingroup
\@namedef{the#1}{\arabic{#1}}%
\addtocounter{#1}{\m@ne}%
\refstepcounter{#1}%
\label{#2}%
\endgroup}
\makeatother
\newenvironment{prob}[1][]%
{\setcounter{partCounter}{0}
\renewcommand{\probName}{#1}%
\edef\numberofparts{\getrefnumber{noPart:\arabic{probCounter}}}%
\newcommand{\abc}{\ref{noPart:\arabic{probCounter}}}%
\section{Problem \arabic{probCounter}: \probName} (contains
\ifnum\numberofparts=0
1 part%
\else
\ifnum\numberofparts=1
1 part%
\else
\numberofparts\ parts%
\fi
\fi)%
}{\howmany{noPart:\arabic{probCounter}}%
\stepcounter{probCounter}%
}
\newcommand{\partName}{}%
\renewenvironment{part}[1][]%
{\stepcounter{partCounter}%
\renewcommand{\partName}{#1}%
\subsection{{Part \Alph{partCounter}}{: \partName}}%
}{}
\newenvironment{ques}[1][]{%
\ifthenelse{%
\equal{#1}{}
}{}{%
\ifthenelse{%
\equal{#1}{1}%
}{%
\textbf{(#1 point)}%
}{%
\textbf{(#1 points)}%
}}%
}{}
\begin{document}
\begin{prob}[Prob 1]
\begin{part}
\end{part}
\begin{part}
\end{part}
\begin{part}
\end{part}
\end{prob}
\begin{prob}[Prob 2] %%%%%%%%%%% force to show => (contains 1 part)
\end{prob}
\begin{prob}[Prob 3] %%%%%%%%%%% correct the grammar => (contains 1 part)
\begin{part}
\end{part}
\end{prob}
\begin{prob}[Prob 4]
\begin{part}
\end{part}
\begin{part}
\end{part}
\begin{part}
\end{part}
\begin{part}
\end{part}
\end{prob}
\end{document}