0

So I am trying to create a new package for Latex. Currently I have one function like this:

\def\timeline#1#2#3{%
   % draw the timeline
}%

In this function I open the tikz environment with \begin{tikzpicture}. I also have a function which draws a single event into this environment, which can be called multiple times.

Now I am not sure how to close the tikz environment after all events have been added. Can somebody explain this to me?

Currently I have the following code in the sty file:

\newenvironment{cvtimeline}{%
    \begin{tikzpicture}%
}{%
    \end{tikzpicture}%
}%
% Functions
\def\timeline#1#2#3{%
   % draw the timeline
}%

\def\work#1#2#3#4#5{%
   % add an work event and draw
}%
\def\school#1#2#3#4#5{%
   % add an school event and draw
}%

This can be used in a tex file as follows:

\begin{document}
    \begin{cvtimeline}
        \timeline{2009}{2017}{\textwidth}
        \work{2009}{8}{2013}{8}{Hello}
        \work{2013}{10}{2014}{8}{World}
    \end{cvtimeline}
\end{document}
Pascal
  • 779
  • If you want to create a LaTeX package, you should use LaTeX syntax. – Johannes_B Feb 18 '17 at 10:02
  • @Johannes_B and this means? – Pascal Feb 18 '17 at 10:03
  • As far as I understood, you want to create a command, which opens an environment and another command which closes the environment? I think, what @Johannes_B refers to is \newcommmand instead of \def. I also often use def, because I'm a lazy prick. – MaestroGlanz Feb 18 '17 at 10:22
  • 1
    You can use \newenvironment instead of \def (or \newcommand) to define your timeline: \newenvironment{timeline}[3]{... code to open your environment...}{... code to close you envrionment...} – Paul Gaborit Feb 18 '17 at 10:27
  • 1
    In my opinion the command \timeline is defined according to LaTeX syntax. It is defined using \def which is TeX, but considering that the majority of packages uses this convention I don't see a major problem, as long as you are sure that the command is not earlier defined. But it could also be that @Johannes_B encourage you to use LaTeX \newenvironment for your environment. That will make the closing much easier. – StefanH Feb 18 '17 at 10:31
  • So if I create a new environment with \newenvironment the I can draw the timeline. But how do I define the command to create a new event? Do I do this with def or what is the equivalent in LaTeX syntax? Because I found a lot of packages that use def, so I am sorry if this is wrong. – Pascal Feb 18 '17 at 10:45
  • 1
    Pascal, i think there are some packages already around to draw timelines. Are you sure you want to add one on top of that? – Johannes_B Feb 18 '17 at 11:13
  • @Johannes_B Yeah, I know there are some packages around. I also looked into them. But they do not do what I want. I created a LaTex file which does what I want. Now I just want to pack it into a sty file for easier usage. – Pascal Feb 18 '17 at 11:15
  • It is hard to suggest syntax when you have shown no example of any relevant code or intended use but it sounds like you should be defining an environment. If \timeline is defined as above and \endtimeline is defined to be \end{tikzpicture} and whatever else is needed the use would be \begin{timeline}{a}{b}{c}.... \end{timeline} – David Carlisle Feb 18 '17 at 11:29
  • I added some more code, which perhaps explains my intention better. – Pascal Feb 18 '17 at 11:39

1 Answers1

1

Structure your code as

document:

\documentclass{article}

\usepackage{mytimeline}

\begin{document}

    \begin{cvtimeline}{2009}{2017}{\textwidth}
        \work{2009}{8}{2013}{8}{Hello}
        \work{2013}{10}{2014}{8}{World}
    \end{cvtimeline}

\end{document}

package mytimeline.sty

\RequirePackage{tikz}

\newenvironment{cvtimeline}[3]{%
    \begin{tikzpicture}%
    % the code that you had in \timeline
}{%
    \end{tikzpicture}%
}%
% Functions
%\def\timeline#1#2#3{%
%   % draw the timeline
%}%

\def\work#1#2#3#4#5{%
   % add an work event and draw
}%
\def\school#1#2#3#4#5{%
   % add an school event and draw
}%
David Carlisle
  • 757,742