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.

I am trying to make a new function, to make my life simpler. The code is supposed to make a pagebreak and ouput a colored box at the next page. Aswell as puting something in the TOC The code works, but not while inside of a command. Does anyone know how to do this?

The ultimate setup would be a command like this

\DEL{1}

producing

\newpage
\phantomsection
\addcontentsline{toc}{section}{{\color{red}\Large{Del 1}}}
\fcolorbox{black}{LightSteelBlue}{\parbox[t][1.25cm][c]{\textwidth}
{\begin{center}\textbf{Del 1 \\Uten hjelpemiddler}\end{center}}}  }

and

\Del{2}

Producing

\newpage
\phantomsection
\addcontentsline{toc}{section}{{\color{red}\Large{Del 2}}}
\fcolorbox{black}{LightSteelBlue}{\parbox[t][1.25cm][c]{\textwidth}
{\begin{center}\textbf{Del 1 \\Uten hjelpemiddler}\end{center}}}  }

I tried to make a new command like

\newcommand{\Del}{ 
\newpage
\phantomsection
\addcontentsline{toc}{section}{{\color{red}\Large{Del 2}}}
\fcolorbox{black}{LightSteelBlue}{\parbox[t][1.25cm][c]{\textwidth}
{\begin{center}\textbf{Del 1 \\Uten hjelpemiddler}\end{center}}}  }
}

Alas this gives me some strange error, that I have not yet begun my document.

Now, I think it is all of the commands such as newpage latex does not want inside of a command. Looking forward to see your clever ideas, to solve this problem =)



Now, thanks to the effort below. I was able to discover what I was looking for an almost solve it.

Now this function is ment to be used on tests and exams as a header. "Uten hjelpemidler" means without any assistance/tools. Just pen and paper.

While "Med hjelpemiddler" is with all tools avaible, computer calculator etc. Now the function below is supposed to count each time I use such a header and count up. The input argument is for saying wheter the section is with or without calculator.

The problem is that the counter "tittel" gives an error when placed inside of the function. Anyone know how to fix this ? =)

\documentclass[border=5pt,tightpage]{standalone}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{xstring}

\newcounter{tittel}
\renewcommand{\thetittel}{\arabic{tittel}}
\newcommand{\tittel}{\stepcounter{tittel} \thetittel }
\setcounter{tittel}{0}

\newcommand{\Del}[1]{%
  \IfStrEqCase{#1}{%
    {u}{\def\Label{Uten }}%
    {m}{\def\Label{Med }}%
    }[\def\Label{UNKNOWN}]%
 % \newpage
  \phantomsection
  \addcontentsline{toc}{section}{%
    \protect\textcolor{red}{\protect\Large{Del \tittel }}}
  \noindent\fcolorbox{black}{LightSteelBlue}{%
    \parbox[t][1.25cm][c]{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{
      \centering\bfseries Del \tittel \\ \Label hjelpemider}%
  }%
}

\begin{document}

Del{u}

Del{m}

Del{u}

\end{document}    

Removing the \ infront of tittel, makes the command run. So adding a counter inside the command makes it fail =(

share|improve this question
    
If Del is supposed to accept parameters the number of these need to be specified as [2]. Then you access them as #1, #2. You should also add a % and the end of each line. –  Peter Grill Nov 11 '11 at 23:48
    
Your snipplet has one closing } moren then opening {. Can you provide the error message you get and a MWE? –  knut Nov 11 '11 at 23:51
1  
A MWE would have been really helpful here- otherwise people have to make educated guesses as to which packages to load :) –  cmhughes Nov 12 '11 at 0:02
add comment

2 Answers

up vote 4 down vote accepted

Since a complete MWE with the appropriate packages, was not provided, I made some adjustments to get this to compile, but this is probably what you are looking for:

enter image description here

The [1] in the \newcommand specifies how many paramters the Del macro is to expect. Then to access this parameter in the command you use #1. Similarily if there were two parameters, you would use [2] and they would be accessed as #1 and #2, etc.. You can have up to 9 parameters for a \newcommand.

The % should be added at the end of each line to ensure that extra spaces do not get inserted. Try removing them to see what happens without them. I leanerd this the hard way as per Tex Capacity Exceeded (if remove % after use of macro), so I would recommend that you should get into the habit of including the %.

\documentclass[border=5pt,tightpage]{standalone}
\usepackage{xcolor}
\usepackage{hyperref}

\newcommand{\Del}[1]{%
  \newpage%
  \phantomsection%
  \addcontentsline{toc}{section}{{\color{red}\Large{Del #1}}}%
  \fcolorbox{black}{blue!10}{\parbox[t][1.25cm][c]{\textwidth}%
  {\begin{center}\textbf{Del #1 \\Uten hjelpemiddler}\end{center}}}%
}

\begin{document}
\Del{1}

\Del{2}
\end{document}

If I understand your comment you want the title to be different depending on if {1} or {2} is used. If so you can use the xstring package to handle the various cases. Here I do what you expect for the two cases and print UNKNOWN if a character other than {1} or {2} is provided:

enter image description here

\documentclass[border=5pt,tightpage]{standalone}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{xstring}

\newcommand{\Del}[1]{%
  \IfStrEqCase{#1}{%
    {1}{\def\Label{Uten hjelpemiddler}}%
    {2}{\def\Label{Med hjelpemidler}}%
    }[\def\Label{UNKNOWN}]%
  \newpage%
  \phantomsection%
  \addcontentsline{toc}{section}{{\color{red}\Large{Del #1}}}%
  \fcolorbox{black}{blue!10}{\parbox[t][1.25cm][c]{\textwidth}%
  {\begin{center}\textbf{Del #1 \\\Label}\end{center}}}%
}

\begin{document}
\Del{1}
\Del{2}
\Del{3}
\end{document}
share|improve this answer
    
Ah! So close! Seems like I was just a few %`s and } away from the answer. The only problem is that when using 1 it should use "uten hjelpemiddler" and when using 2 it should use "med hjelpemidler" Is there any way to acount for this? If there is I would happily accept your answer =) –  N3buchadnezzar Nov 11 '11 at 23:57
    
Sure, that can be done, but are there only 2 cases, or are there more? –  Peter Grill Nov 12 '11 at 0:04
    
Actually four of your % are redundant. :) –  egreg Nov 12 '11 at 0:12
    
@egreg: Yeah, but isn't just easier to always include them so you don't need to think about it? –  Peter Grill Nov 12 '11 at 0:13
    
I have updated the solution. Please let me know if this is what you were looking for. –  Peter Grill Nov 12 '11 at 0:18
show 4 more comments
\newcommand{\DEL}[2]{%
  \newpage
  \phantomsection
  \addcontentsline{toc}{section}{%
    \protect\textcolor{red}{\protect\Large{Del #1}}}
  \noindent\fcolorbox{black}{LightSteelBlue}{%
    \parbox[t][1.25cm][c]{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{
      \centering\bfseries Del #1 \\ #2}%
  }%
}

Call as

\DEL{1}{Uten hjelpemiddler}

Note that not only some % characters were missing, but the \parbox was too large and also shifted right by the indentation.

It's better to use \centering than the center environment inside the \parbox; other pairs of braces can be eliminated as well by declarations such as \bfseries, that are confined in a box.

share|improve this answer
    
Very nice solution =) Question though. I treid simply changing the name of the function from DEL to various other things DEL1 , Del , DL1 , DELen all gives me an error. And will not run the command. Whilst Drude , Dru , Drd will all run just fine. Why is this? –  N3buchadnezzar Nov 12 '11 at 0:36
    
@N3buchadnezzar You can't use numbers in command names, only letters. (This is not completely true, but it's a quite good approximation of the truth.) –  egreg Nov 12 '11 at 0:42
add comment

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.