Using \ifthenelse
statements I've defined a macro that prints it's value as long as it is different from the value of the previous time it was used.
\RequirePackage{xifthen}
\def\storedval{} % create a macro to later store a value in
\newcommand{\mycommand}[1]{%
\ifthenelse{\isempty{#1}{}}{\\}{% If empty, don't print anything but keep an empty line.
\ifthenelse{\equal{#1}{\storedval}}{\\}% If value is the same as the previous one, don't print it but keep an empty line.
{#1\\}\def\storedval{#1}}} % Else: print value and store it in \storedval
The purpose for this is that I get data generated by another program but do not want to print the data if it is repeated. In the document, this:
\mycommand{A}
\mycommand{A}
\mycommand{A}
\mycommand{}
\mycommand{B}
\mycommand{C}
\mycommand{C}
\mycommand{A}
… gives me A, three empty lines, B, C, an empty line, and A. That's what I want.
Now I would like to store this in a package and define an option that would let me choose whether or not I want to print repeated lines. I couldn't figure out how to use \DeclareOption
properly. I could of course repeat the whole code, and place it inside of two \DeclareOption
statements. But I assume there's a more economical way.
So I gave it a shot trying the following MWE, but this is clearly the wrong way (outcommented the lines that are not working). (Disclaimer: this is the first time I'm trying to declare an option for a package …)
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{mypackage.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}
\RequirePackage{xifthen}
\def\storedval{} % create a macro to later store a value in
\newcommand{\mycommand}[1]{%
\ifthenelse{\isempty{#1}{}}{\\}{% If empty, don't print anything but keep an empty line.
%\DeclareOption{skiprepetitions}{\ifthenelse{\equal{#1}{\storedval}}{\\}}% If value is the same as the previous one, don't print it but keep an empty line.
%\DeclareOption{keeprepetitions}{} % don't do anything if this is selected
{#1\\}\def\storedval{#1}}} % Else: print value and store it in \storedval
\DeclareOption*{\PackageWarning{mypackage}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
\end{filecontents}
\usepackage{mypackage}
%\usepackage[skiprepetitions]{mypackage}
\begin{document}
Testing:\\
\mycommand{A}
\mycommand{A}
\mycommand{A}
\mycommand{}
\mycommand{B}
\mycommand{C}
\mycommand{C}
\mycommand{A}
\mycommand{A}
\end{document}
Best Answer
Here's a fully working package which does what you want. It uses
expl3
and related packages and you have three ways of using your commands: you can specify a package option globally, use a setup command locally or pass an optional argument to one of the macros.Note that the second A in the example is produced by passing an optional macro parameter.