Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to get the following document to work

\documentclass[10pt]{article}
\usepackage{enumitem}

\newcounter{foo}
\newcounter{bar}[foo]
\addtocounter{foo}{1}

\usepackage{xparse}
\usepackage{xstring}

\NewDocumentEnvironment{\PF}{m o}{
  \IfStrEqCase{#1}{%
    {0}{\def\Label{\alph*) }}%
    {1}{\def\Label{\phantom{\alph*) }}%
    }[\def\Label{\alph*}]}%
    \IfValueTF{#2}{ \stepcounter{foo} \setcounter{bar}{1} }{}%
    { \stepcounter{bar} \textbf{\Roman{foo}. Problem \arabic{bar} }}%     
    \begin{enumerate}[label = \Label]}{\end{enumerate}}

\begin{document}

\begin{PF}{1}
\item hello
\end{PF}

\begin{PF}{0}
\item 
\end{PF}

\begin{PF}{1}[2]
\item hello
\end{PF}

\end{document}

But the compiling keeps failing and I am unable to see my error. I want to define an environment called PF with which I can select which label to use in the enumerate environment. PF also defines a counter that is incremented each time, until the optional environment is used. The output I want is

I. Problem 1 
        Hello 

I. Problem 2 
     a) Howdy

II. Problem 1 
     a) foobar

but alas =(

share|improve this question
    
Please, make the example self contained, by loading the needed packages. Also, you should keep in mind that stepping fasitdel will set fasitopg to zero. –  egreg Mar 24 '13 at 15:04
    
Soemthing like this? I am uncertain of which packages that are missing, since the document will not compile (which is why I am asking this question..) =) –  N3buchadnezzar Mar 24 '13 at 15:13
1  
Well, of course enumitem is mandatory, but also xstring, don't you think? Change \PF into PF. –  egreg Mar 24 '13 at 15:17
add comment

1 Answer

up vote 5 down vote accepted

Note the changes added as comments; I also removed some spaces.

\documentclass[10pt]{article}
\usepackage{enumitem,xstring,xparse}

\newcounter{foo}
\newcounter{bar}[foo]
\stepcounter{foo} % better then \addtocounter

\NewDocumentEnvironment{PF}{m o}{% not \PF!
  \IfStrEqCase{#1}{%
    {0}{\def\Label{\alph*) }}%
    {1}{\def\Label{\protect\phantom{\alph*) }}% note \protect
    }[\def\Label{\alph*}]}%
    % bar is stepped only once and \noindent is added
    \IfValueT{#2}{\stepcounter{foo}}% no need of a false branch
    {\noindent\stepcounter{bar}\textbf{\Roman{foo}. Problem \arabic{bar}}}%
    \begin{enumerate}[label = \Label]}{\end{enumerate}}


\begin{document}

\begin{PF}{1}
\item hello
\end{PF}

\begin{PF}{0}
\item hello
\end{PF}

\begin{PF}{1}[2]
\item hello
\end{PF}

\end{document}

enter image description here


I'd use a key-value syntax, instead.

\documentclass[10pt]{article}
\usepackage{xstring,enumitem}
\newcounter{fasitdel}
\newcounter{fasitopg}[fasitdel]
\addtocounter{fasitdel}{1}
\addtocounter{fasitopg}{1}

\usepackage{xparse}

\ExplSyntaxOn
\keys_define:nn {PF}
 {
  label .bool_set:N = \l_pf_label_bool,
  label .default:n = false,
  step  .bool_set:N = \l_pf_step_bool,
  step  .default:n = false,
 }

\NewDocumentEnvironment{PF}{O{}}
 {
  \keys_set:nn {PF} { #1 }
  \bool_if:NT \l_pf_step_bool
   {\stepcounter{fasitdel}\stepcounter{fasitopg}}
  \noindent\textbf{\Roman{fasitdel}.~Oppgave~\arabic{fasitopg}}
  \stepcounter{fasitopg}
  \bool_if:NTF \l_pf_label_bool
   {\begin{enumerate}[label = \alph*)~]}
   {\begin{enumerate}[label = \protect\phantom{\alph*)~}]}
 }
 {\end{enumerate}}
\ExplSyntaxOff

\begin{document}

\begin{PF}[label=true]
\item hello
\end{PF}

\begin{PF}
\item Howdy 
\end{PF}

\begin{PF}[label=true,step=true]
\item foo 
\end{PF}

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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