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}
egreg
  • 1,121,712
Adobe
  • 3,037
  • 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
  • @Werner: but code I post is smater: it changes color of the section name after each section. – Adobe Nov 14 '13 at 22:04
  • @Adobe: And? You can adapt the code in the those answers to change the colour with every section. That wasn't the point of that question. – Werner Nov 14 '13 at 22:06
  • @Werner: You right: issuing \sectionfont before each section manually solves the problem. But this brings a problem of turning off section numbers with sectsty. – Adobe Nov 14 '13 at 22:14
  • \setcounter{secnumdepth}{0} – egreg Nov 14 '13 at 22:21
  • 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
  • I don't understand why \@sectioncolor has three arguments. It seems you want to color the section title, but what else? Try deleting the arguments #2 and #3. Then, in the definition of \section, change the line \@sectioncolor #1% to \@sectioncolor{#1}% (with braces). If that doesn't do what you want, explain what it is you want. – Dan Nov 15 '13 at 06:38
  • @Dan: it gives ! Argument of \@ has an extra }. – Adobe Nov 15 '13 at 09:01
  • @Adobe You've solved it, but I should point out that the error about \@ has nothing to do with your coloring problem. You simply shouldn't use command names with \@ in them in your document. Or, if you must, then surround the code that includes them with \makeatletter...\makeatother. – Dan Nov 15 '13 at 21:52

2 Answers2

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

egreg
  • 1,121,712
  • 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
  • If the section title appears as the first thing on a new page, this doesn't work as expected: the color for that section title will be always gray and not the next in the specified list – Aelius Mar 30 '23 at 12:44
  • @Aelius Not in my experiment: I added \clearpage before \section{eeeeee} and it comes out red as expected. – egreg Mar 30 '23 at 12:51
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}
Adobe
  • 3,037
  • 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 08: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