-1

I'd like to form a "running sheet" for an event that is likely to change, so I'd like LaTeX to keep track of the calculations for me if the programme changes. I imagined storing a "start" time, having a counter "duration" set for each section, and using LaTeX to add these two so that the result is still a valid clock time for the start of the next section. This would continue for each element in sequence. For example, 2pm "start", 10 minute intro "duration", leads to a printed time for the next item of 2:10pm (the pm is not necessary). That way, if someone decides they can/cannot give a speech as part of the opening, we don't have the tedious business of changing all of the timings. I also don't want to have to manually ensure that the passing of 60 minutes actually increments the hour.

I've looked at datetime2 and some other packages, and it's not obvious how you would add to the stored date, but I admit that normally I use R for imposing some requirements on date formats. I'd prefer to not have to have an R script to do this and then import the text to a LaTeX doc. Any suggestions to create something like what I describe are welcome.

Werner
  • 603,163
Jason Whyte
  • 1,408
  • So this will be specific to a single day event? Do you want a 12-hour or 24-hour clock representation? – Werner May 25 '17 at 03:55
  • @Werner, immediate concern is a single-day event that doesn't go past midnight, so no need for variable date. For Australia, 12 hour clock is more standard in civilian life so that is preferred. Solution for a multi-day event is not yet a concern, but might be quite useful in the near future. – Jason Whyte May 25 '17 at 03:59
  • 1
    It would be helpful to show us what you have tried. Posting a minimal working example that indicates what you are trying to do makes it easier for people to understand what you want. It also makes it easier for people to help you, since they have some code to start from and, consequently, much more likely that some one will try to help you. Currently I think that the chances of some one answering are small because you have not given a very precise description of what you want and you are asking some one to do everything for you. –  May 25 '17 at 04:10
  • 2
    Member for over four (!) years and no MWE? -1 – Henri Menke May 25 '17 at 04:12
  • I can appreciate that lazy posters are tiring for the community. I put an amount of research in and couldn't find any appropriate package. I am not asking someone to write code for me in the first instance. I just don't know what I don't know, and if someone could say "Use package XYZ" that would be sufficient. Why reinvent the wheel? In the past I've had a go at trying to put a solution together. Here, I know that currently I don't have the technical expertise to solve the anticipated problems outlined in my original post, which demonstrates that I have thought about potential issues. – Jason Whyte May 25 '17 at 04:18
  • @JasonWhyte Ah, I see. Sorry, voting to close as opinion-based then. – Henri Menke May 25 '17 at 06:15
  • Are you an R + LateX user? Do you know Sweave or knitr ? And Rstudio? You do not must " have an R script to do this and then import the text to a LaTeX", just a single filename.Rnw file with Latex code including the R chunks between lines with <<>>= and @, so you write "a <- 2+3; a" but is printed just "5" or you can use a LaTeX commands as \Sexpr{a+60} where a+60 is obviously a R (S) expression using the a from previous R code. There are many examples in this site about this. Compilation sequence (.Rnw > .tex > .pdf) is done automatically with Rstudio. – Fran May 25 '17 at 06:43
  • @Fran I have never combined R and Latex, so I wouldn't have known to look into Sweave or knitr, and I appreciate you taking the time to point out these possibilities. – Jason Whyte May 25 '17 at 12:10
  • @JasonWhyte https://tex.stackexchange.com/a/191035/36296 may be what you are looking for. – samcarter_is_at_topanswers.xyz May 25 '17 at 17:00

2 Answers2

2

The following might be a sufficient start:

enter image description here

\documentclass{article}

\usepackage{xfp}

\makeatletter
% Split time into hours and minutes
\def\@splittime#1:#2\relax{\edef\@timehour{#1}\edef\@timeminute{#2}}
% Set start time: \starttime{<hour>:<minute>}
\newcommand{\setstarttime}[1]{%
  \@splittime#1\relax%
  \xdef\@starttime{\two@digits\@timehour:\two@digits\@timeminute}}
% Print start time
\newcommand{\printstarttime}{\@starttime}
% Set duration of event: \setduration{<hours>:<minutes>}
\newcommand{\setduration}[1]{%
  \@splittime#1\relax%
  \edef\@durationhour{\@timehour}\edef\@durationminute{\@timeminute}%
  \@addduration}
% Adds duration to start time
\newcommand{\@addduration}{%
  \expandafter\@splittime\@starttime\relax
  \edef\@timehour{\fpeval{\@timehour+\@durationhour+trunc((\@durationminute+\@timeminute)/60)}}%
  \edef\@timehour{\fpeval{\@timehour-12*(trunc((\@timehour-1)/12))}}%
  \edef\@timeminute{\fpeval{\@timeminute+\@durationminute}}%
  \edef\@timeminute{\fpeval{\@timeminute-60*(trunc(\@timeminute/60))}}%
  \xdef\@starttime{\two@digits\@timehour:\two@digits\@timeminute}%
}
\makeatother  

\begin{document}

Start: \setstarttime{8:12}\printstarttime

+0:15 = \setduration{0:15}\printstarttime

+2:17 = \setduration{2:17}\printstarttime

+5:55 = \setduration{5:55}\printstarttime

+0:21 = \setduration{0:21}\printstarttime

+6:60 = \setduration{6:60}\printstarttime

+1:120 = \setduration{1:120}\printstarttime

\end{document}

You set a start time using \setstarttime{<hour>:<minute>} where the : is required. Then you can add an event duration via \setduration{<hours>:<minutes>}; again, the : is required, and you can set some odd things here as well (like 1:120, which denotes an event spanning 1 hour and 120 minutes, or 3 hours). \printstarttime prints the start time using a 12-hour clock and two-digit printing.

No consideration is made for PM or AM, but that could be added. A respectable input interface would be required though (perhaps \setstarttime{8:27AM}). Also, it is assumed that a correct start time will be set, although one could also check that the user input this correctly if needed. Other extensions may also be possible, together with the necessary error checking.

Werner
  • 603,163
2

You don't need to know any LaTeX package. Use simply TeX primitives (or plain TeX macros):

\newcount\hr \newcount\mn  \newcount\tmpnum

\def\ntime{\tmpnum=\mn 
   \divide\tmpnum by60 \advance\hr by\tmpnum
   \multiply\tmpnum by60 \advance\mn by-\tmpnum
   \tmpnum=\hr \divide\tmpnum by12 
   \multiply\tmpnum by12 \advance\hr by-\tmpnum
   \ifnum\hr=0 \hr=12 \fi
}
\def\setstarttime#1{\setstarttimeA#1&}
\def\setduration #1{\setdurationA #1&}
\def\setstarttimeA#1:#2&{\hr=#1\relax \mn=#2\relax \ntime}
\def\setdurationA #1:#2&{\advance\hr by#1\relax \advance\mn by#2\relax \ntime}
\def\printstarttime{\ifnum\hr<10 0\fi \the\hr:\ifnum\mn<10 0\fi \the\mn}

Start: \setstarttime{8:12}\printstarttime

+0:15 = \setduration{0:15}\printstarttime

+2:17 = \setduration{2:17}\printstarttime

+5:55 = \setduration{5:55}\printstarttime

+0:21 = \setduration{0:21}\printstarttime

+6:60 = \setduration{6:60}\printstarttime

+1:120 = \setduration{1:120}\printstarttime

\bye
wipet
  • 74,238