4

The following def takes first three letters as an argument:

\newcounter{colorCounter}
\def\@sectioncolor#1#2#3{%
  {%
    \color{%
      \ifcase\value{colorCounter}%
        blue\or%
        red\or%
        orange\or%
        green\or%
        purple\or%
        brown\else%
        headercolor\fi%
    } #1#2#3%
  }%
  \stepcounter{colorCounter}%
}

It actually colors first three letters of the section name. How do I make it color the whole section name instead?

Can I make it to take the whole paragraph as an argument?

Edit

In order to make it work one has also to

\renewcommand{\section}[1]{
  \par\vspace{\parskip}
  {%
    \LARGE\headingfont\color{headercolor}%
    \@sectioncolor #1%
  }
  \par\vspace{\parskip}
}

Edit 2

MWE:

\documentclass{article}

\usepackage{fontspec}
\usepackage{polyglossia}

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text}
\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}

\usepackage{xcolor}
\colorlet{headercolor}{gray}

\newcounter{colorCounter}
\def\@sectioncolor#1#2#3{%
  {%
    \color{%
      \ifcase\value{colorCounter}%
        blue\or%
        red\or%
        orange\or%
        green\or%
        purple\or%
        brown\else%
        headercolor\fi%
    } #1#2#3%
  }%
  \stepcounter{colorCounter}%
}

\renewcommand{\section}[1]{
  \par\vspace{\parskip}
  {%
    \LARGE\color{headercolor}%
    \@sectioncolor #1%
  }
  \par\vspace{\parskip}
}

\begin{document}


\section{aaaaaa}

\section{bbbbbb}

\section{cccccc}

\end{document}
  • 2
    This doesn't color anything by itself. I don't find \@sectioncolor in latex.ltx nor in (x)color.sty. Without a minimal example it's impossible to say anything sensible. – egreg Nov 14 '13 at 21:53
  • What is \color{headercolor}? It is really confusing code. – kiss my armpit Nov 14 '13 at 21:58
  • @DonutE.Knot: it's \colorlet{headercolor}{gray} – Adobe Nov 14 '13 at 21:59
  • @egreg: see edit. – Adobe Nov 14 '13 at 22:00
  • 2
    @Adobe: I'm hoping at this point you can actually reword the question. A title such as "Paragraph as an argument" doesn't seem to be what you're after. Perhaps what you're after is a way to format the colour of a section heading that cycles through a list of colours... – Werner Nov 14 '13 at 22:21
5

Use sectsty (or, with some more work, titlesec):

\documentclass{article}

\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{sectsty}

\colorlet{headercolor}{gray}

\newcounter{colorCounter}
\newcommand\sectioncolor{%
    \color{%
      \ifcase\value{colorCounter}%
        blue\or
        red\or
        orange\or
        green\or
        purple\or
        brown\else
        headercolor\fi
  }%
  \stepcounter{colorCounter}%
  % Remove the following line if you don't want to cycle
  \ifnum\value{colorCounter}=7 \setcounter{colorCounter}{0}\fi
}

% in section titles \sectioncolor will be executed
\sectionfont{\sectioncolor}

% don't number sections
\setcounter{secnumdepth}{0}

\begin{document}

\section{aaaaaa}

\section{bbbbbb}

\section{cccccc}

\section{dddddd}

\section{eeeeee}

\section{ffffff}

\section{gggggg}

\section{hhhhhh}

\end{document}

enter image description here

  • That's cool solution for a problem, but I hack moderncv template, and have to make as little modifications as possible. – Adobe Nov 15 '13 at 11:26
2

Oh I solved it:

\documentclass{article}

\usepackage{fontspec}
\usepackage{polyglossia}

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text}
\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}

\usepackage{xcolor}
\colorlet{headercolor}{gray}

\newcounter{ColorCounter}
\newcommand\Color{
  \color{%
    \ifcase\value{ColorCounter}%
      blue\or%
      red\or%
      orange\or%
      green\or%
      purple\or%
      brown\else%
      headercolor%
    \fi%
  }
}

\renewcommand{\section}[1]{
  \par\vspace{\parskip}
  {
    \LARGE\Color #1
  }
  \par\vspace{\parskip}
  \stepcounter{ColorCounter}
}

\begin{document}


\section{aaaaaa}

\section{bbbbbb}

\section{cccccc}

\end{document}
  • Of course you're not worried about the section title ending up at the bottom of a page and the text in the next one. – egreg Nov 15 '13 at 10:26
  • You mean there is a potential buggy behaivior? I'd've read Your book on programming latex, but You've written it in Italian. – Adobe Nov 16 '13 at 8:16
  • You don't take any precaution, so there's a feasible page break point after your section titles. – egreg Nov 16 '13 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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