# How to create same text structure every time?

My document contains 'blocks of text' with the same structure. They always have a title (which is always a date), a review part and a to do part. This is what the code looks like:

\documentclass[11pt]{article}

\usepackage{enumerate}

\newenvironment{review}
{\textbf{Review:} \begin{itemize}}
{\end{itemize}}
\newenvironment{todo}
{\textbf{To do:} \begin{itemize}}
{\end{itemize}}

\begin{document}

\section*{30th November 2013}
\begin{enumerate}[1)]
\item blabla
\end{enumerate}

\begin{review}
\item blabla
\end{review}

\begin{todo}
\item blabla
\end{todo}
\end{document}


I was wondering if it's possible to omit the need to type this structure every time and create a command or environment called Article in order to save time and organize my document a little bit better. How can I do this?

-
You could create a two-part environment, with a \todo macro splitting it in the middle. One question: where is the title in your example? – Jubobs Nov 30 '13 at 14:14
@Jubobs: The title is the \section* part. I don't know whether this is a good way to define a title, but it works for now. Anyway, the title is always a date. – Jeroen Nov 30 '13 at 14:16
@Jubobs: Of course I had the intention to mark an answer as the solution, but I was really busy today. – Jeroen Dec 1 '13 at 22:51

Here is an idea: a three-part environment called Article, whose only (mandatory) argument is the date (specified as {<day>}{<month>}{<year>}).

To start the Review part, simply use my \review macro. To start the To do part, simply use my \todo macro. It may be wise to rename those macros to avoid clashes with existing macros; for instance, I know that the todonotes package defines a macro called \todo.

For more maintainability, you may also want to use a package for typesetting dates, such as Nicola Talbot's datetime, which I used in my example. By doing so, you won't need to hardcode the date (as 30th November 2013 in your example) and you will be able to change the way it should be typeset at any stage.

\documentclass[11pt]{article}

\usepackage{enumerate}
\usepackage[long,nodayofweek]{datetime}

\newenvironment{Article}[1]
{%
\newcommand\review
{%
\end{enumerate}
\textbf{Review:}
\begin{itemize}
}
\newcommand\todo
{%
\end{itemize}
\textbf{To do:}
\begin{itemize}
}
\section*{\formatdate#1}
\begin{enumerate}[1)]
}{%
\end{itemize}
}

\begin{document}

\begin{Article}{{30}{11}{2013}}
\item foo
\item bar
\review
\item foo
\item bar
\todo
\item foo
\item bar
\end{Article}

\end{document}

-

Once again this is a bit more than what you are asking for, but this is an ideal situation where using keys can stream-line things very nicely (after an initial overhead of constructing them).

I take a four pronged approach here.

1. I define the keys I will use.

2. I define a macro for the user interface. In this case \myarticle that takes one argument for the key-value pairs. This macro then calls another macro for handling the formatting.

3. I define an internal macro \my@article that determines what code to execute. This is a bit of a middleman, but can be very nice for quickly reformatting, changing the order of presentation, or just debugging.

4. For each aspect of the final environment I define a macro to handle the formatting. Each of these macros follows the naming convention \jeroen@build@.... There's one defined for the date, another for review, and a third for to do. These are the macros that you would redefine if you want to change the presentation of the content.

For the keys, I illustrate three different approaches:

• Use of .initial to use pgfkeys internal mechanism for storing and recalling a key value.
• Use of .store in to store the value of the key in a user defined macro.
• Use of .code to execute some code when a particular key is called.

I feel .code is the most interesting of these in the current context because, in the following MWE, it will allow the user to opt out of using a review list. Also, for front matter I do something similar: the key value is passed as for the other keys--as though it were a list--but its final formatting is just as a normal paragraph.

Here's the MWE:

\documentclass{article}
%%-------------------------------------------
%% packages you need for this to work
\usepackage{pgfkeys}
\usepackage{pgffor}
%%-------------------------------------------
%% package OP requested
\usepackage{enumitem}
%%-------------------------------------------
\makeatletter
%%-------------------------------------------
%% These next two macros are not entirely
%% necessary.  I put them here to illustrate
%% further possibilities about how pgfkeys
%% can manage keys.
\def\jeroen@review@items{}
\def\jeroen@to@do@items{}
\newif\ifjeroen@review@
\newif\ifjeroen@front@matter@
%% define the keys
\pgfkeys{/jeroen/article/.cd,
date/.initial=,
front matter/.code=\jeroen@front@matter@code{#1},
review items/.code=\jeroen@review@code{#1},
to do items/.store in=\jeroen@to@do@items,
}
%% define the macro for user interface
\newcommand\myarticle[1]{%%
\bgroup
\pgfkeys{/jeroen/article/.cd,#1}%%
\build@my@article
\egroup
}
\def\jeroen@front@matter@code#1{%%
\jeroen@front@matter@true
\def\jeroen@front@matter@content{#1}}
\def\jeroen@review@code#1{%%
\jeroen@review@true
\def\jeroen@review@items{#1}}

%% an internal macro for initially handling
%% and formatting the content.
\def\build@my@article{%%
\jeroen@build@date
\jeroen@build@front@matter
\jeroen@build@review
\jeroen@build@to@do
}
%% macro for formatting the "date"
%% If you want to change how the "date" is presented
%% this is the code you should tweak.
\def\jeroen@build@date{%%
\section*{\pgfkeysvalueof{/jeroen/article/date}}}
%% macro for formatting the "front matter"
%% If you want to change how the "front matter" is
%% presented this is the code you should tweak.
\def\jeroen@build@front@matter{%%
\ifjeroen@front@matter@
\foreach \mystuff in \jeroen@front@matter@content
{\mystuff\space}%%
\vspace{2ex}\par
\fi
}
%% macro for working with the list of "review" items
%% This is the code you should tweak to change how
%% the "review" is formatted.
\def\jeroen@build@review{%%
\ifjeroen@review@
\noindent
\textbf{Review:}
\begin{enumerate}%%
\foreach \reviewitem in \jeroen@review@items
{%%
\item \reviewitem
}%%
\end{enumerate}
\fi
}
%% macro for working iwth the list of "to do" items
%% This is the code you should tweak to change how
%% the "to do" is formatted.
\def\jeroen@build@to@do{%%
\noindent
\textbf{To do:}
\begin{itemize}
\foreach \todoitem in \jeroen@to@do@items
{%%
\item \todoitem
}%%
\end{itemize}
}
\makeatother

\begin{document}

\myarticle{
date={November 29, 2013},
review items={
{turkey},
{stuffing},
{yams}
},
to do items={
{apples},
{pears},
{peaches},
{ice cream}}
}

\myarticle{
date={November 30, 2013},
front matter={
{The quick brown fox jumped over the lazy dog.},
{Pack my box with five dozen liquor jugs.}},
to do items={
{carrots},
{celery},
{parsnips},
{ice cream!!}}
}

\end{document}


I should note a couple things:

• The brackets in date={November 30, 2013} are necessary to prevent pgfkeys from thinking the comma in the date is the separator for the keys.
• The other keys should be formatted as: { {item 1} , {item 2} , {item 3} , ...}

Granted, this is a lot of initial work to do. But in the end it provides with a great deal of latitude for how you finally format the content.

-

A recommended solution only for best practitioners.

## Create a package

As you want to reuse the environments as well as the items in many projects, the best solution is to put all of them in a single package. Save the package in a directory accessible to LaTeX (it will be explained later if you don't know how).

In the following example, I chose mytodolist.sty as the name for the package. You can change it if you want to.

% mytodolist.sty
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mytodolist}[2013/12/01 v0.01 LaTeX package for my consistent todo list]

\RequirePackage{enumerate}

\newenvironment{review}
{\textbf{Review:} \begin{itemize}}
{\end{itemize}}
\newenvironment{todo}
{\textbf{To do:} \begin{itemize}}
{\end{itemize}}

\newcommand{\Section}[1][\today]{\section*{#1}}

% items that will be reused many times
\newcommand{\foo}{\item foo}
%\newcommand{\bar}{\item bar} % it cannot be made as \bar is already defined!
\newcommand{\baz}{\item baz}

\endinput


If you have new items to add, just edit this package. Use \newcommand rather than \def to make sure that the existing macros don't get redefined.

## Use the package

Now you can use the package as follows.

% main.tex

\documentclass[preview,border=12pt,12pt,varwidth]{standalone}% change it back to your own document class
\usepackage{mytodolist}

\begin{document}
\Section
\begin{review}
\foo
%\bar
\baz
\end{review}

\begin{todo}
\foo
%\bar
\baz
\end{todo}

\Section[Dec 25, 2013]
\begin{review}
\foo
%\bar
\baz
\end{review}

\begin{todo}
\foo
%\bar
\baz
\end{todo}
\end{document}


## Simulation 2 in 1

The following code snippet can be used to simulate the creation of the package on the fly and use it. You should not do it for your production scenario.

\documentclass[preview,border=12pt,12pt,varwidth]{standalone}% change it back to your own document class

\usepackage{filecontents}

\begin{filecontents*}{mytodolist.sty}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mytodolist}[2013/12/01 v0.01 LaTeX package for my consistent todo list]

\RequirePackage{enumerate}

\newenvironment{review}
{\textbf{Review:} \begin{itemize}}
{\end{itemize}}
\newenvironment{todo}
{\textbf{To do:} \begin{itemize}}
{\end{itemize}}

\newcommand{\Section}[1][\today]{\section*{#1}}

% items that will be reused many times
\newcommand{\foo}{\item foo}
%\newcommand{\bar}{\item bar} % it cannot be made as \bar is already defined!
\newcommand{\baz}{\item baz}

\endinput
\end{filecontents*}

\usepackage{mytodolist}

\begin{document}
\Section
\begin{review}
\foo
%\bar
\baz
\end{review}

\begin{todo}
\foo
%\bar
\baz
\end{todo}

\Section[Dec 25, 2013]
\begin{review}
\foo
%\bar
\baz
\end{review}

\begin{todo}
\foo
%\bar
\baz
\end{todo}
\end{document}

-