3

I am trying to keep a journal-type file using TeX. I would like to create a template in which I need not manually enter the date each day. Example:

January 26, 2014 Entry for the day here.

January 27, 2014 Entry for the day here.

January 28, 2014 Entry for the day here.

I would ideally like to automate this to:

\today <--- Set date to January 26, 2014 that does not change when compiling next day Entry for the day here.

\today <--- Set date to January 27, 2014 that does not change when compiling next day Entry for the day here.

\today <--- Set date to January 28, 2014 that does not change when compiling next day Entry for the day here.

Any help would be greatly appreciated! Best wishes, ~Joco

  • 5
    Welcome to TeX.SX! This is simply not possible. TeX reads a text file and can't know when you made a note; this is to be done at the editor's level. – egreg Jan 27 '14 at 18:56
  • I wouldn't go so far as to say it is impossible, but it may be far more risky to automate such a procedure rather than specifying a day manually. For example, what should happen when you consider to skip a day of writing and write two days in one? – Werner Jan 28 '14 at 6:23
2

I've seen so many crazy things made with LaTeX, i wouldn't say that something is impossible, but you have to use LuaLaTeX for my solution.

At first make a file,call it datetime.lua and add the following line.

datetime.lua

t = {}

All dates are saved in a lua table. You can edit them later in this file.

main.tex

\documentclass{scrartcl}
\usepackage{luacode}

\begin{luacode}
 require("datetime")

 function getdate(c)
  d = os.date("%d %B, %Y")   
   if (c <= #t) then
    tex.print(t[c])
   else
    file = io.open("datetime.lua", "a")
    file:write("t[".. c .."] = '" .. d .. "'; ")
    file:flush()
    file:close()
    tex.print(d)
   end
 end
\end{luacode}

\newcounter{date}
\addtocounter{date}{1}

\newcommand{\customdate}{\directlua{getdate(\number\value{date})}\stepcounter{date}}

\begin{document}

\customdate

\end{document}

I am using a counter to make a referenz to the date position. The value is passed to a simple lua function. If the date is already saved, it is loaded from the table and if not, it is written to the datetime file and the current date is printed.

Edit: You can get a problem when you change the order. (reorder the numbers in datetime.lua)

2

Well, here is one way to achive what you want, but this requires that you:

  • Add any new entries at the bottom (ie., your file's entries are in the order that you added them).
  • Run pdflatex everytime you add a new entry.
  • Keep the \jobname.db which keeps track of what each of the \today's refer to.

So, any earlier uses of \today (in this example these are the first two) get marked in blue), and today's entry shows up in red:

enter image description here

Further Enhancements:

  • Define a \yesterday macro in case you don't get around to adding the enteries until after midnight for some reason.

Code:

\documentclass{article}

\usepackage{datatool}
\usepackage{xstring}
\usepackage{tikz}
\usetikzlibrary{calc}

%%http://tex.stackexchange.com/questions/179425/a-new-command-of-the-form-tex
\newcommand{\CenteredText}[2][]{%
\noindent\tikz \draw [draw=black, ultra thick, #1]
    ($(current page.center)-(0.5\linewidth,0)$) -- 
    ($(current page.center)+(0.5\linewidth,0)$)
    node [midway, fill=white] {#2};
}

\let\OldToday\today
\newcounter{CountOfPriorHistory}

\newcommand*{\TempMonthAndDay}{}%
\newcommand*{\TempYear}{}%
\newcommand*{\LoadHistory}{%
    \IfFileExists{\jobname.db}{%
        \DTLloaddb[keys={TodayCount,MonthAndDay,Year}]{HistoryDB}{\jobname.db}%  Read prior DB
    }{%
        \DTLnewdb{HistoryDB}%           Initialize new DB (this is first time usage)
    }%
    \renewcommand{\today}{% Get data from HistoryDB while we can
        \stepcounter{CountOfPriorHistory}%
        \ifnum\arabic{CountOfPriorHistory}>\DTLrowcount{HistoryDB}\relax
            % Then this is a new entry 
            \StrBefore{\OldToday}{,}[\TempMonthAndDay]%
            \StrBehind{\OldToday}{,}[\TempYear]%
            \dtlexpandnewvalue%
            \DTLnewrow{HistoryDB}%
            \DTLnewdbentry{HistoryDB}{TodayCount}{\arabic{CountOfPriorHistory}}%
            \DTLnewdbentry{HistoryDB}{MonthAndDay}{\TempMonthAndDay}%
            \DTLnewdbentry{HistoryDB}{Year}{\TempYear}%
            \DTLsavedb{HistoryDB}{\jobname.db}%
            \bigskip\par\CenteredText[thin,red]{\textbf{\OldToday}}%
        \else
            % This is an older entry, so need to retrieve the date for it
            \DTLgetvalue{\HistoryMonthAndDay}{HistoryDB}
                    {\arabic{CountOfPriorHistory}}
                    {\dtlcolumnindex{HistoryDB}{MonthAndDay}}%
            \DTLgetvalue{\HistoryYear}{HistoryDB}
                    {\arabic{CountOfPriorHistory}}
                    {\dtlcolumnindex{HistoryDB}{Year}}%
            \bigskip\par\CenteredText[thin,blue]{\textbf{\HistoryMonthAndDay, \HistoryYear}}%
        \fi
    }%
}%
\AtBeginDocument{\LoadHistory}



\begin{document}

\today
Well, today I saw this problem at TeX.SE that some say can't be done.
Of course this make me want to tackle it.

\today
Had an idea as to how to tackle it, but since I am flying out of the country I can't quite work on it.

\today
Now that I have done enough procrastinating on TeX.Se, I can do some more by working on that
so-called "unsolvable problem".  I think I have a solution ready to post.

\end{document}
0

Put every entry in a file of its own. Use a consistent naming scheme; I’ll use entryinteger.tex below to work well with platform-independent pgffor, but you could also do standard ls (Unix, Linux, OS X) or dir (DOS, Windows) with --shell-escape (see also package ifplatform). This could look something like this:

\documentclass{minimal}
\usepackage{pgffor} % \foreach
\usepackage{filemod} % \Filemodtoday etc., does not work with xetex
\def\inputentries#1{% takes the max number of entries as an argument
  \foreach \i in {1,2,...,#1}{%
    \IfFileExists{entry\i.tex}{%
      \section{\Filemodtoday{entry\i.tex}}
      \input{entry\i}%
    }{}%
  }
}
\begin{document}
\inputentries{9}
\end{document}

Alas, I assume you want everything in one file. That won’t work.

  • 3
    Also correcting a typo in one file on a later day than its creation would produce a wrong date. – egreg Jan 27 '14 at 22:04

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.