2

I have the following code:

\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}

enter image description here

Now I wanna it to be:

...

Prob 2 (contains 1 part) %% force it to be 1 part

Prob 3 (contains 1 part) %% get rid of the "s" from the wording (part"s")

...

The first thing come to my mind is conditional ifthenelse function, which also included in my code, but it doesn't work as expected.

1

The problem is that \ref{...} is not a number, but a set of instruction for printing a number.

You can use the refcount package that provides \getrefnumber that expands to 0 if the reference has not yet been established or to the reference itself.

I've made some other small changes (in particular protected several end-of-lines). For part/parts the syntax with \ifthenelse is cumbersome and I preferred using directly \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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.