Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Introduction

I created a simple bus timetable with TikZ:

enter image description here

Currently, the node placing is done manually.

The problem

I would like to create an environment which would look approximately like this:

\begin{timetable} % optional argument could be [6-21]
    \direction{Sutjeska to Nahorevo:}{6:30,7:15,8:00,9:00}[2em]
\end{timetable}

It would:

  • draw as much time as is needed -- (rounded up)-1 and (rounded up)+1 for first start time and last start time respectively (for this example 6-10)
  • if the optional argument is given, the start time and end time are as given by the argument
  • draw the times given in a list at the appropriate place (so 7:15 is at ($(hour7)!0.25!(hour8)$)
  • yshift the direction title and times by the amount given in \direction{<direction title>}{<timetable>}[<amount>]

What I would like to know

How can I split the <timetable> argument and perhaps store it some list. I could then probably search for minimum, maximum and calculate appropriate positions for the placing times on the timetable.

share|improve this question
2  
The arguments of \newcommand are not the arguments of the macro defined by it, so "\newcommand argument" is incorrect here. What you mean is the argument of a custom defined macro. –  Martin Scharrer Jun 16 '11 at 12:38
    
@Martin: Corrected, thank you. –  ipavlic Jun 16 '11 at 12:49

2 Answers 2

up vote 5 down vote accepted

You can use the PGF/TikZ macro \foreach to loop over the comma separated list. The colon can be split by a macro with a particular parameter text as shown below:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\def\splitcolon#1:#2\relax{%
    % #1 : hour
    % #2 : minutes
    % Do something with them, e.g. store them in macros.
    \def\hour{#1}%
    \def\minutes{#2}%
}

\newcommand{\direction}[2]{%
    \foreach \v in {#2} {%
        % expand \v so that it can be split at the colon:
        \expandafter\splitcolon\v\relax
        % use the values as you wish:
        (\hour)(\minutes)\\ % dummy example
    }%
}

\direction{Sutjeska to Nahorevo:}{6:30,7:15,8:00,9:00}

\end{document}
share|improve this answer
    
Is there a list "datatype" in which I could store my hours and minutes (like array, vector, or list)? –  ipavlic Jun 16 '11 at 12:58
    
@ipavlic: Not really, just separated lists as strings. See the etoolbox package for a few list specific macros. –  Martin Scharrer Jun 16 '11 at 13:05
2  
@ipavlic: If you really want a "datatype" you could look in to the pgfoo object oriented stuff (see the PGF manual, v2.10 or later), but in this case it might be overkill. –  Loop Space Jun 16 '11 at 13:09
    
@ipavlic if you really want data types proper, I think LaTeX3 has some capacity, and there's always LuaTeX... –  Seamus Jun 16 '11 at 13:29
    
Thank you for your suggestions. I don't need a datatype for this example, but it could be useful otherwise. –  ipavlic Jun 16 '11 at 14:36

TeX allows almost an infinite variety of data structures, but it leaves the methods to manipulate them for you.

For example, this object:

\timetable{Nahorevo}{Sutjeska}{12:50,14:45,15:35,16:25}

holds information for the route from Nahrorevo to Sutjeska and is an "object" containing two "strings" and a "list".

The most useful data structure is a list and my own preference is to use built-in LaTeX routines for manipulating lists.

We first define a macro to hold information for a route:

\def\route#1#2#3{%
  \expandafter\def\csname#1#2\endcsname{#1 to #2}
  \expandafter\def\csname#1#2times\endcsname{#3}
}

Then we define a macro to loop over the information and print the timetable.

\def\timetable#1#2#3{%
  \route{#1}{#2}{#3}
  \def\XX{\csname#1#2times\endcsname}
  \csname#1#2\endcsname\par
  \@for \i:=\XX \do{%
     \i,
   }
}

We can then call the two routes as:

\timetable{Sutjeska}{Nahorevo}{6:30,7:15,8:00,9:00}\par
\timetable{Nahorevo}{Sutjeska}{12:50,14:45,15:35,16:25}

Why I created commands for the routes, is that it is entirely possible that if one had an algorithm for the route Sutjeska->Nahorevo, the Nahorevo->Sutjeska could have been determined automatically, hence creating commands can give you more flexibility in the long run.

The full minimal is shown below,

    \documentclass{article}
    \begin{document}
    \makeatletter
    \def\route#1#2#3{%
      \expandafter\def\csname#1#2\endcsname{#1 to #2}
      \expandafter\def\csname#1#2times\endcsname{#3}
    }
    \def\timetable#1#2#3{%
      \route{#1}{#2}{#3}
      \def\XX{\csname#1#2times\endcsname}
      \csname#1#2\endcsname\par
      \@for \i:=\XX \do{%
         \i,
       }
    }

   \timetable{Sutjeska}{Nahorevo}{6:30,7:15,8:00,9:00}\par
   \timetable{Nahorevo}{Sutjeska}{12:50,14:45,15:35,16:25}
   \makeatother
   \end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.