3

I'm using Spundun's method for automating inputing the entries in a diary, in turn inspired by Peter Grill (Using LaTeX to keep a diary), and I would like to add section dividers by year. This should be easily achieved by introducing an if-then clause with an existential quantifier in it, but I don't know how to do this.

Clarification about directory structure: The directory is of the form /Year/Month/Day.tex (e.g. 2014/Jan/25.tex), and each of the entries starts with \mytitle{Title}. The main .tex file is in the father directory of \Year.

How I think this could be solved: I thought one way to do what I want would be to introduce a clause like the following: for every /year, if there's a file in /Year/Month, input the file /Year.tex —which would include only a line: \section{Year} (say \section{2014}). Unfortunately, I'm not familiar enough with LaTeX to implement the solution.

Here's the code I'm using:

\documentclass{tufte-book}
\usepackage{tikz}
\usepackage{xifthen}

\title{Title}
\author{Author}

\newenvironment{loggentry}[2]%date, heading
{\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}

\pgfmathtruncatemacro{\StartYear}{2014}
\pgfmathtruncatemacro{\EndYear}{2016}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]
{   \ifthenelse{\writetitle=1}{#1}{}
}

\newread\mysource

\begin{document}
\maketitle

\foreach \Year in {\StartYear,...,\EndYear}
{   \foreach \Month in {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}
    {   \foreach \Day in {1,...,31}
    {       \openin\mysource=\Year/\Month/\Day.tex
                \read\mysource to \firstline
                \closein\mysource
                \xdef\writetitle{1}
                \begin{loggentry}{\Year - \Month - \Day}{\firstline}    
                    \xdef\writetitle{0}
                    \input{\Year/\Month/\Day}
                \end{loggentry} 
    }
    {   % files does not exist, so nothing to do
    }

    }  
  }
}

\end{document}

Here's what I would like the result to look like: journal

Thanks!

  • Welcome to TEX.SX. Can you provide a screen shot or sketch how this document should look like? The naming scheme 'Year/Month/Day' with '/' separators leads to problems in Linux, for example – user31729 Jul 30 '14 at 5:17
  • Where is \mytitle called? Within the included .tex file? – user31729 Jul 30 '14 at 5:23
  • In addition to my first comment: Is there a directory for each day, in separate months, for separate years? – user31729 Jul 30 '14 at 5:27
  • Thanks! I added clarification of the directory structure to the question. But yes, there is a main directory called "Diary" and has a directory for each year, which itself has directories for each month, each of which has a file .tex, named by the day number. – Questionbeggar Jul 30 '14 at 13:24
  • See my updated solution please – user31729 Jul 30 '14 at 21:31
2

A trial of a solution...

I generated dummy directories with year/month/#day.tex files, just a file called #year.tex, i.e. 2014.tex etc. containing the line \section{#year} etc, whereas the #day.tex files contain \mytitle{Some Stuff}\blindtext

In the most inner Foreach loop, I defined \CurrentFileName with the structure as above and use the \LaTeX macro \IfFileExists to be on the safe side, if this is true, then the \openin code from the OP is used.

The OP clearified the question such that there is a 2014.tex etc. in 2014 etc. which contains something like \section{2014}. This should only be included, if there is at least one file in 2014/{Jan,.....,Dec}, otherwise dropped. I tried to achieve this by a status counter variable.

\documentclass{tufte-book}
\usepackage{tikz}
\usepackage{blindtext}%
\usepackage{etoolbox}%


\title{Title}
\author{Author}

\newenvironment{loggentry}[2]%date, heading
{\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}

\pgfmathtruncatemacro{\StartYear}{2014}
\pgfmathtruncatemacro{\EndYear}{2016}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]
{  
  \ifnumequal{\writetitle}{1}{#1}{}%
}

\newcommand{\InputIfExists}[1]{%
\IfFileExists{#1}{\input{#1}}{}%
}%

\newcounter{includedcntr}
\setcounter{includedcntr}{0}%

\newread\mysource

\begin{document}
\maketitle

\foreach \Year in {\StartYear,...,\EndYear}
{%
  \setcounter{includedcntr}{0}%  Year file is not included so far
  \foreach \Month in {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}
  {%   
    \foreach \Day in {1,...,31}
    {% 
      \def\CurrentFileName{\Year/\Month/\Day.tex}%
      \IfFileExists{\CurrentFileName}{%
        %%% Test if any of the expected day files *.tex exists, if true, then
        %%% include the year file and prevent a further inclusion by setting includedcntr to 1
        \ifnumequal{\number\value{includedcntr}}{0}{%
          \InputIfExists{\Year/\Year.tex}%
          \setcounter{includedcntr}{1}}%
        {}%
        % Now read the title
         \openin\mysource=\CurrentFileName%
         \read\mysource to \firstline
         \closein\mysource
         \xdef\writetitle{1}
        \begin{loggentry}{\Year - \Month - \Day}{\firstline}    
          \xdef\writetitle{0}
          \input{\CurrentFileName}\par

        \end{loggentry} 
      }{}%
    }% Day - foreach
  }% Month -foreach
}% Year - foreach

\end{document}%

Here is a small Linux bash script, that generates the files in the local directory:

#!/bin/bash

YEARS="2014 2015 2016"

MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

for i in $YEARS
do
 mkdir $i 2> /dev/null
 echo -e "\\\\section{$i}\n" > $i/$i.tex
 for j in $MONTHS
 do
   mkdir -p $i/$j 2> /dev/null
   for k in `seq 1 31` 
   do
     echo -e "\\\\mytitle{Some stuff}\n\\\\blindtext" > $i/$j/$k.tex
   done
 done
done

And here is some of the output

enter image description here

  • Thanks Christian! I tried to run it but it gave me "Undefined control sequence". I'll check it out tonight to see if I just missed a } when copying. The answer you offer here seems to add a section title after each log entry (unless that's just part for the dummy text). I added a screenshot of the expected result to the original question. – Questionbeggar Jul 30 '14 at 13:28
  • @Questionbeggar: Sorry, I missed a package in my code, which was not obvious, since the relevant command is not called at all. I updated the solution and will look after the section you mentioned. – user31729 Jul 30 '14 at 14:28
  • @Questionbeggar: Ok, happy TeXing then ;-) – user31729 Jul 30 '14 at 22:47

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.