Suppose I have my own counter as in


and now I want to start a new "section" based on the value of x. For example, if x==-1 I want \part, and if x==0 then it will be \chapter and if x==1 it will be \section and so on.

I do not want to write each time something like this (do not even know it is valid syntax)

   \ifnum \value{x} \eq  -1
        \ifnum \value{x} \eq 0

I'd like to just write

  \makeSectionTypeBasedOnThisValue{x}{title of the section of chapter is here}

and this command will map to the correct command based on the value of x. I will make sure x is always between -1 and 5 since these are the valid values. I am using book style.

I have a reason to want to do it this way. (briefly, I am building my large tree of latex documents, combining them to one document, and I want to be able to build it starting from any level going down. Hence depending on where in the tree I start the build, section numbering will be different. One time the same level can be a \section while some other time that level can become a \subsection. So I do not want to hardcode the \section or \chapter commands in Latex files, but I want these to depend on the level the file is in the tree. The counter will represent the level in the tree I am starting the build from)

Is there is an easy way to do this? I am a newbie in Latex.

  • Did you try something like \def\Sect#1#2{\ifcase#1\or \part\or\chapter\or\…\fi\relax #2}? If you want to start counter from -1, you'll need to increment in before \ifcase or do other check.
    – Eddy_Em
    Jan 30, 2013 at 5:07
  • @Eddy_Em, no I have not. This is advanced for me. As I said I am newbie in Latex programming. Will try it now and see. thanks.
    – Nasser
    Jan 30, 2013 at 5:09

1 Answer 1


Using \ifcase, you can selectively step through possible numeric values:

enter image description here

  \ifcase\value{myseccntr}% -1
  \or % 0
  \or % 1
  \or % 2
  \or % 3
  \or % 4
  \or % 5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .