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.

Suppose I have my own counter as in

\newcounter{x}
\setcounter{x}{0}

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
         \part             
   \else
        \ifnum \value{x} \eq 0
           \chapter
        ...
        \fi
  \fi
  ...

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.

share|improve this question
    
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 '13 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 '13 at 5:09
add comment

1 Answer 1

up vote 3 down vote accepted

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

enter image description here

\documentclass{report}
\newcounter{myseccntr}
\newcommand{\makeSectionTypeBasedOnThisValue}[1]{%
  \setcounter{myseccntr}{\numexpr#1+1}%
  \ifcase\value{myseccntr}% -1
    \expandafter\part
  \or % 0
    \expandafter\chapter
  \or % 1
    \expandafter\section
  \or % 2
    \expandafter\subsection
  \or % 3
    \expandafter\subsubsection
  \or % 4
    \expandafter\paragraph
  \or % 5
    \expandafter\subparagraph
  \fi%
}
\begin{document}
\tableofcontents
\makeSectionTypeBasedOnThisValue{-1}{Part}
\makeSectionTypeBasedOnThisValue{0}{Chapter}
\makeSectionTypeBasedOnThisValue{1}{Section}
\makeSectionTypeBasedOnThisValue{2}{Subsection}
\makeSectionTypeBasedOnThisValue{1}{Section}
\makeSectionTypeBasedOnThisValue{-1}{Part}
\makeSectionTypeBasedOnThisValue{0}{Chapter}
\makeSectionTypeBasedOnThisValue{1}{Section}
\end{document}
share|improve this answer
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.