3

I have the following self defined counter and command:

\documentclass{article}

\newcounter{usecase}[section]
\newcommand{\usecasenumber}{\arabic{usecase} \stepcounter{usecase}
\newcommand{\usecase}[9]{
    \begin{tabular}{| p{4cm} | p{8cm} |}
        \hline
        Anwendungsfall-Nummer       & U-\usecasenumber  \\
        \hline
        Hauptakteur                 & #1                \\
        \hline
        Andere Akteure              & #2                \\
        \hline
        Beschreibung                & #3                \\
        \hline
        Vorbedingung                & #4                \\
        \hline
        Nachbedingung               & #5                \\
        \hline
        Ausführung                  & #6                \\
        \hline
        Alternativen                & #7                \\
        \hline
        Ausnahmen                   & #8                \\
        \hline
        Benutzte Anwendungsfälle    & #9                \\      
        \hline
    \end{tabular}
}

\begin{document}
\usecase{1}{2}{3}{4}{5}{6}{7}{8}{9}
\end{document}

I now want an environment named usecase that does the exact same thing my newcommand does plus adding another line to the table with the name that one should be able to give the usecase environment. The environment should be able to automatically increase the counter and I want to be able to add labels to the usecase environment (just like it is possible in figure or table). When I reference such a label with \ref I want the reference to be the number of the environment given by the counter usecase.

I am not sure how to realize that or if it is even possible. I would start with something like this but I don't know how to proceed from there on to achieve the desired behaviour.

\newenvironment{usecase}[1]{\refstepcounter{usecase}}{
    % TODO
}

It would be really appreciated if someone could help me out here. :)

  • Welcome to TeX - LaTeX! – Andrew Swann Apr 7 '16 at 11:21
  • Please put your code in a small compilable document (MWE) along with a skeleton for what you want in addition, so that folks here can download it to work on without having to do any extra work. – Ethan Bolker Apr 7 '16 at 12:41
  • I don't think that an environment is necessary here. \refstepcounter works in a command as well. If a caption is needed, this is something different, of course – user31729 Apr 7 '16 at 13:56
3

I don't suggest to use an environment here, but I provided the solution anyway for this.

But I don't recommend using a 9 arguments, but applying a key-value interface to grab the values -- it's not necessary to remember which argument stands for which content then.

\documentclass{article}

\usepackage[utf8]{inputenc}

\newcounter{usecase}[section]

\usepackage{xkeyval}

\makeatletter
\define@key{usecase}{nummer}{%
  \def\kvnummer{#1}%
}

\define@key{usecase}{akteur}{%
  \def\kvakteur{#1}%
}

\define@key{usecase}{andere}{%
  \def\kvandere{#1}%
}

\define@key{usecase}{beschreibung}{%
  \def\kvbeschreibung{#1}%
}

\define@key{usecase}{nachbedingungen}{%
  \def\kvnachbedingung{#1}%
}

\define@key{usecase}{vorbedingungen}{%
  \def\kvvorbedingung{#1}%
}

\define@key{usecase}{ausfuehrung}{%
  \def\kvausfuehrung{#1}%
}

\define@key{usecase}{alternativen}{%
  \def\kvalternativen{#1}%
}

\define@key{usecase}{ausnahmen}{%
  \def\kvausnahmen{#1}%
}

\define@key{usecase}{anwendungen}{%
  \def\kvanwendungsfaelle{#1}%
}

\makeatother

\newenvironment{usecase}[1][]{%
  \refstepcounter{usecase}%
  \setkeys{usecase}{#1}%
  \begin{tabular}{| p{4cm} | p{8cm} |}
    \hline
    Anwendungsfall-Nummer       & U-\theusecase  \\
    \hline
    Hauptakteur                 & \kvakteur                \\
    \hline
    Andere Akteure              & \kvandere                \\
    \hline
    Beschreibung                & \kvbeschreibung                \\
    \hline
    Vorbedingung                & \kvvorbedingung                \\
    \hline
    Nachbedingung               & \kvnachbedingung                \\
    \hline
    Ausführung                  & \kvausfuehrung                \\
    \hline
    Alternativen                & \kvalternativen                \\
    \hline
    Ausnahmen                   & \kvausnahmen                \\
    \hline
    Benutzte Anwendungsfälle    & \kvanwendungsfaelle               \\      
    \hline
  \end{tabular}
}{}

\newcommand{\usecaseother}[1][]{%
  \refstepcounter{usecase}%
  \begingroup 
  \setkeys{usecase}{#1}%
  \begin{tabular}{| p{4cm} | p{8cm} |}
    \hline
    Anwendungsfall-Nummer       & U-\theusecase  \\
    \hline
    Hauptakteur                 & \kvakteur                \\
    \hline
    Andere Akteure              & \kvandere                \\
    \hline
    Beschreibung                & \kvbeschreibung                \\
    \hline
    Vorbedingung                & \kvvorbedingung                \\
    \hline
    Nachbedingung               & \kvnachbedingung                \\
    \hline
    Ausführung                  & \kvausfuehrung                \\
    \hline
    Alternativen                & \kvalternativen                \\
    \hline
    Ausnahmen                   & \kvausnahmen                \\
    \hline
    Benutzte Anwendungsfälle    & \kvanwendungsfaelle               \\      
    \hline
  \end{tabular}
  \endgroup
}


% Preset default values
\presetkeys{usecase}{%
  andere={},
  akteur={},
  anwendungen={},
  vorbedingungen={keine},
  nachbedingungen={viele},
  beschreibung={Wichtig!},
  ausnahmen={keine!},
  alternativen={Absolut keine!},
  ausfuehrung={dringend!}}{}


\begin{document}

In \ref{ucfirst} werden sehen, daß der bessere Fall durch \ref{ucsecond} repräsentiert wird!


\begin{usecase}[akteur={Mr. Gumby}, andere={Mr. Gumbies}] \label{ucfirst}
\end{usecase}

\usecaseother[akteur={Mrs. Ann Elk}, andere={--}, beschreibung={Theorie der Brontosaurier}, ausfuehrung={gehoben!}] \label{ucsecond}
\end{document}

enter image description here

  • 1
    ++1 for the default values (how do I say that in German?) – Ethan Bolker Apr 7 '16 at 15:35
  • Thanks. "Voreinstellungen" or "voreingestellte Werte" (Werte= values) – user31729 Apr 7 '16 at 15:38
  • 1
    Not your fault, but an empty environment makes little sense. – egreg Apr 7 '16 at 18:00
  • 1
    @egreg: That's why I did not recommend it, of course. If it would be a floating one, the situation would be different – user31729 Apr 7 '16 at 18:05

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.