New environment with parameter

by hlapointe   Last Updated August 14, 2019 15:23 PM

This is my first question on this forum, so don't hesitate to correct me. I want to create a new environment who I can pass a parameter "color". This environment call a new command named \sectionColored who take a color and a string. This command will color the first word at a specific color. Can you help me?


EDIT 03 :

\ProvidesClass{cv-style}[2015/02/27 CV class]
\NeedsTeXFormat{LaTeX2e}

\ProcessOptions\relax
\LoadClass{article}


%-------------------------------------------------------------------------------------
% Colors
%-------------------------------------------------------------------------------------
\RequirePackage{xcolor}

\definecolor{white}{RGB}{255,255,255}

\definecolor{gray}{HTML}{4D4D4D}
\definecolor{lightgray}{HTML}{999999}
\definecolor{darkgray}{HTML}{333333}

\definecolor{blue}{HTML}{0395DE}
\definecolor{red}{HTML}{DC3912}
\definecolor{orange}{HTML}{FF9900}
\definecolor{green}{HTML}{109618}
\definecolor{purple}{HTML}{9358FE}
\definecolor{aquamarine}{HTML}{0C343D}

\definecolor{date}{HTML}{A4A4A4}

\colorlet{fillheader}{white}
\colorlet{header}{darkgray}
\colorlet{headerSub}{darkgray}
\colorlet{textcolor}{gray}
\colorlet{headercolor}{gray}
\colorlet{headerSub}{darkgray}


%-------------------------------------------------------------------------------------
% Fonts
%-------------------------------------------------------------------------------------
\RequirePackage[quiet]{fontspec}
\RequirePackage[math-style=TeX,vargreek-shape=unicode]{unicode-math}

\newfontfamily\bodyfont
[BoldFont=texgyreheros-bold.otf,
ItalicFont=texgyreheros-italic.otf,
BoldItalicFont=texgyreheros-bolditalic.otf]
{texgyreheros-regular.otf}
\newfontfamily\bodyfont{Roboto-Regular}[Path=fonts/]
\newfontfamily\bodyfontit{Roboto-LightItalic}[Path=fonts/]
\newfontfamily\thinfont{Roboto-Thin}[Path=fonts/]
\newfontfamily\headingfont{RobotoCondensed-Bold}[Path=fonts/]

\defaultfontfeatures{Mapping=tex-text}
\setmainfont[Mapping=tex-text, Color=textcolor, Path = fonts/]{Roboto-Light}

\newcommand{\italica}[1]{%
    {\color{gray}\bodyfontit #1}%
}


%-------------------------------------------------------------------------------------
% Header
%-------------------------------------------------------------------------------------
\RequirePackage{tikz}
\usetikzlibrary{positioning}

\newcommand{\rolefont}{%
  \fontsize{14pt}{24pt}\selectfont%
  \thinfont%
  \color{white}%
}

\newcommand{\header}[3]{%
  \begin{tikzpicture}[remember picture,overlay]
    \node [rectangle, fill=fillheader, anchor=north, minimum width=\paperwidth, minimum height=3cm] (box) at (6.2, 1.8){};
    \node [anchor=center] (name) at (box) {%
      \fontsize{30pt}{62pt}\color{header}%
      {\thinfont #1}{\bodyfont  #2}
    };
    \node [below=of name.west, anchor=west] at (0, 0.5) {%
      \fontsize{14pt}{24pt}\color{headerSub}%
      \bodyfont #3%
    };
  \end{tikzpicture}
  \vspace{0.6cm}
  \vspace{\parskip}
}

\newcommand{\darkbar}{
  \\\fcolorbox{white}{gray}{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{%
  .....
  }}
}


%-------------------------------------------------------------------------------------
% Structure
%-------------------------------------------------------------------------------------
\RequirePackage{parskip}
\setlength{\parskip}{4pt}

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

\newcommand{\sectioncolored}[3]{%
  \section{\textcolor{#1}{#2} #3}%
}

\renewcommand{\subsection}[2]{
  \par\vspace{.5\parskip}%
  \Large\headingfont\color{headercolor} #2%
  \par\vspace{.25\parskip}%
}

\pagestyle{empty}


%-------------------------------------------------------------------------------------
% List environment
%-------------------------------------------------------------------------------------
\setlength{\tabcolsep}{0pt}

\makeatletter
\def\entrylistsplitfirst#1 #2\@nil{#1}
\def\entrylistsplitsecond#1 #2\@nil{#2}
\newenvironment{entrylist}[2]{%
  \edef\tempa{\expandafter\expandafter\expandafter\entrylistsplitfirst #2\@nil}%
  \edef\tempb{\expandafter\expandafter\expandafter\entrylistsplitsecond #2\@nil}%
  \sectioncolored{#1}{\tempa}{\tempb}%
  \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}ll}

  \newcommand{\entry}[4]{
    ##1&\parbox[t]{12.5cm}{
      \textbf{##2}
      \hfill
      {\footnotesize\addfontfeature{Color=#1} ##3}\\
      ##4\vspace{\parsep}
    }\\\\
  }
}{
  \end{tabular*}
}
\makeatother


%-------------------------------------------------------------------------------------
% Side block
%-------------------------------------------------------------------------------------
\RequirePackage[absolute,overlay]{textpos}
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}

\newenvironment{aside}{%
  \let\oldsection\section
  \renewcommand{\section}[1]{
    \par\vspace{0.75\baselineskip}{\Large\headingfont\color{headercolor} ##1}
  }
  \begin{textblock}{4.0}(1, 0.15)
  \begin{flushright}
  \obeycr
}{%
  \restorecr
  \end{flushright}
  \end{textblock}
  \let\section\oldsection
}


%-------------------------------------------------------------------------------------
% Other tweaks
%-------------------------------------------------------------------------------------
\RequirePackage[left=5.6cm,top=1.15cm,right=1cm,bottom=0.90cm,nohead,nofoot]{geometry}
\RequirePackage{hyperref}

With,

\documentclass[]{cv-style}

\begin{document}
%-------------------------------------------------------------------------------------
% Header
%-------------------------------------------------------------------------------------
\header
{Hugo}
{Lapointe Di Giacomo}
{Commerce aux détails \& Service à la clientèle}
\darkbar

%-------------------------------------------------------------------------------------
% Side Block
%-------------------------------------------------------------------------------------
\begin{aside}
\section{Adresse}
28, rue de la Calèche
St-Basile-le-Grand, Qc
Canada
~
\section{Téléphone}
\href{tel:15146544846}{+1 514 654-4846}
~
\section{Adresses électroniques}
\href{mailto:[email protected]}{hugo.lapo\\@\textbf{gmail.com}}
\href{mailto:[email protected]}{hugo.lapointe.di.giacomo\\@\textbf{umontreal.ca}}
~
\section{Profils}
\href{https://ca.linkedin.com/in/hugolapointe}{\textbf{LinkedIn}/\\hugolapointe}
\href{http://www.facebook.com/hugolapointedigiacomo}{\textbf{Facebook}/\\HugoLapointeDiGiacomo}
~
\section{Compétences professionnelles}
\includegraphics[scale=0.62]{img/personal.png}
~
\section{Langages de programmation}
\includegraphics[scale=0.62]{img/programming.png}
~
\section{Intérêts professionnels}
Sciences, Informatique, Mathématiques, Sécurité, Médical, Cinéma, Social.
~
\section{Langues}
\textbf{Français}\includegraphics[scale=0.40]{img/5stars.png}
\textbf{Anglais}\includegraphics[scale=0.40]{img/3stars.png}
\end{aside}


%-------------------------------------------------------------------------------------
% Body
%-------------------------------------------------------------------------------------
\begin{entrylist}{blue}{Expériences de travail}
    \entry
    {2015}
    {Opérateur aux remontées mécaniques}
    {Ski St-Bruno, St-Bruno-de-Montarville}
    {
      Responsable de l’accueil et du contact avec les invités. 
      Opérer les remontées mécaniques. 
      Travailler dans un climat à température extrême. 
      Respect de protocoles de sécurité.
    }

    \entry
    {2014}
    {Stagiaire en génie logiciel}
    {Miramétrix Inc., Montréal}
    {
      Conception orientée objet et respect des patrons de conception. 
      Création d'outils de développement. 
      Production de procédures écrites et de documentations. 
      Respect des normes de programmation strictes et d'échéanciers serrés.
    }

    \entry
    {2013}
    {Programmeur analyste}
    {Groupe Dutailier, St-Pie}
    {
      Conception de sites Web et programmes répondant à des besoins particuliers. 
      Analyse des besoins et production de documentations fonctionnelles. 
      Création et intégration de bases de données. 
      Collaboration avec experts et unification des compétences.
    }

    \entry
    {2010 $-$ 2013}
    {Associé aux ventes}
    {La Source, Beloeil}
    {
      Service de vente destiné à la clientèle. 
      Services techniques en informatique, électrique et mécanique offert à la clientèle. 
      Tâches de gestion telles que l'ouverture et la fermeture du magasin. 
      Surpassement de quotas et d'objectifs de ventes hebdomadaires.
    }

    \entry
    {2008 $-$ 2010}
    {Soldat d'infanterie}
    {Forces Canadiennes, R22eR}
    {
      Travail d'équipe fortement structuré et fonctionnel. 
      Doit effectuer un travail rigoureux et efficace. 
      Accomplissement de tâches physiques et mentales dans des conditions difficiles. 
      Innovation et dépassement de soi.
    }  
\end{entrylist}


\begin{entrylist}{red}{Parcours scolaire}
    \entry
    {Depuis 2015}
    {Baccalauréat en mathématiques et informatique}
    {Université de Montréal}
    {Spécialisation en \italica{génie logiciel} et en \italica{optimisation}.}

    \entry
    {2013 $-$ 2014}
    {Études en génie logiciel}
    {École de technologie supérieure (ÉTS)}
    {Plus de 50 crédits cumulés et un stage complété.}

    \entry
    {2013}
    {DEC en technique informatique}
    {Cégep de St-Hyacinthe}
    {Informatique de gestion}   
\end{entrylist}


\begin{entrylist}{orange}{Formations professionnelles}
    \entry
    {2014}
    {Pompier niveau 1}
    {École Nationale des Pompiers du Québec}
    {
      Sauvetage de victimes, évacuation de bâtiments, recherche de victimes, protection de bâtiments, éradication du feu et circonscription du feu. 
      Interventions en matières dangereuses.
    }

    \entry
    {2014}
    {Secouriste avancé RCR C+}
    {Formation Vitale}
    {
      Interventions de secours et gestion sécuritaire de la situation. 
      Dispenser les premiers secours selon les blessures. 
      Communiquer l'évaluation relative à la situation aux services préhospitaliers d'urgence.
    }  
\end{entrylist}
\end{document}


Answers 1


Like this?

blue and black

\documentclass{article}
\usepackage{xcolor}
\newcommand{\sectioncolored}[3]{%
  \section{\textcolor{#1}{#2} #3}%
}
\makeatletter
\def\entrylistsplitfirst#1 #2\@nil{#1}
\def\entrylistsplitsecond#1 #2\@nil{#2}
\newenvironment{entrylist}[2]{%
  \edef\tempa{\expandafter\expandafter\expandafter\entrylistsplitfirst #2\@nil}%
  \edef\tempb{\expandafter\expandafter\expandafter\entrylistsplitsecond #2\@nil}%
  \sectioncolored{#1}{\tempa}{\tempb}%
}{}
\makeatother
\begin{document}
\begin{entrylist}{blue}{Blue Black}
  abc
\end{entrylist}
\end{document}

Explanation

We need to pass \sectioncolored 3 arguments rather than 2: the colour; the first word; the remaining words. We then use \textcolor{#1}{#2} to colour just the first word.

\newcommand{\sectioncolored}[3]{%
  \section{\textcolor{#1}{#2} #3}%
}

We're going to use @ commands, so we need to change the catcode using \makeatletter.

\makeatletter

We define \entrylistsplitfirst#1 #2\@nil to get the first word of the title.

\def\entrylistsplitfirst#1 #2\@nil{#1}

We define \entrylistsplitsecond#1 #2\@nil to get the remainder of the title.

\def\entrylistsplitsecond#1 #2\@nil{#2}

We need \def because our parameter specification includes a space. The \@nil is used to mark the end of the arguments. Note that these definitions assume that the title will have at least 2 words. If you might have a single-word title, you would need to adjust the code to accommodate this possibility as well.

We then define the environment.

\newenvironment{entrylist}[2]{%

First, we save the first word of the title into the macro \tempa.

  \edef\tempa{\expandafter\expandafter\expandafter\entrylistsplitfirst #2\@nil}%

Second, we save the rest of the title into the macro \tempb.

  \edef\tempb{\expandafter\expandafter\expandafter\entrylistsplitsecond #2\@nil}%

Finally, we pass the colour (#1) and the two parts of the title off to \sectioncolored. Note that I altered something here as the original code defined one macro and used another, which obviously won't work. (\sectioncolored is not the same macro as \sectionColored.)

  \sectioncolored{#1}{\tempa}{\tempb}%
}{}

Finally, we restore @ to its usual status in the preamble.

\makeatother
cfr
cfr
May 02, 2016 19:35 PM

Related Questions





Environment inside \newcommand

Updated April 02, 2016 08:09 AM