5

Using the memoir class, I'm looking for a way to automatically color any string that starts whith an @ character, whether it be in the main text or in a footnote. Any idea how to do that?

Update : I also use bibtex, and i don't want link box or color to my citation, that explain the hidelink option.

With a minimal example :

\documentclass[a4paper, 12pt,twoside, openright]{memoir}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\usepackage{fontspec}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[autostyle=true,french=guillemets,maxlevel=3]{csquotes}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage[protrusion=true]{microtype}

\makeatletter

\renewcommand\@makefnmark{\hbox{\@textsuperscript{\normalfont\color{BurntOrange}\@thefnmark}}}
\renewcommand\@makefntext[1]{%
  \parindent 1em\noindent
            \hb@xt@1.8em{%
                \hss\@textsuperscript{\normalfont\@thefnmark}}#1}
\makeatother

\usepackage[hidelinks, pdfusetitle]{hyperref} % Creates hyperlinks and index in the PDF document, preferably load after biblatex

\begin{document}

\chapterstyle{bringhurst}

This is a link in a footnote \footnote{My link is \href{http://www.google.fr}{@google}}

This is a colored link in my text \href{http://www.google.fr}{@google}

This is a normal link \href{http://www.google.fr}{google}

\end{document}

I want to autodetect and color text link which start with @ and not all the link.

  • 1
    Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – jub0bs Mar 15 '14 at 14:36
  • 2
    The two words in the example that start with @ are the link texts of \href. But you have specified the hyperref option hidelinks, which removes all visible markup of links. – Heiko Oberdiek Mar 15 '14 at 14:52
  • Because i have also citation link using bibtex, and i don't want colored link on it :/ – reyman64 Mar 15 '14 at 15:14
3

REVISION with general solution to problem of coloring all words beginning with a particular symbol, @. RE-EDITED to handle situation of lone @ character. It is done by making @ active, and then defining it to turn on red and print the saved copy of @. The key to this revision is the macro \tilblank that uses recursion to print out the next word (still in red). When it encounters a space, it reverts to black. If it encounters an end-of-group, it exits (and also reverts to black).

\documentclass{article}
\usepackage{ifnextok}
\usepackage{xcolor}
\makeatletter
\def\tilblank#1{#1\IfNextToken\@sptoken{ \color{black}}{%
  \IfNextToken\egroup{}{\tilblank}}}
\def\atend{\IfNextToken\@sptoken{ \color{black}}\tilblank}
\let\svat @
\makeatother
\catcode`@=\active
\def@{\color{red}\svat\atend}
\begin{document}
This tests @the use of @midstream or in its own {@group}.
Also testing @ by itself and at the end of a @paragraph.

This follows in black, as @it should. 
\end{document}

enter image description here


ORIGINAL SOLUTION

I'm not sure if this approach breaks anything else. I make @ active. Also, this only works because your @ words are in their own group. That means if you wanted to use @ in text, it would have to be enclosed in {}.

\documentclass[a4paper, 12pt,twoside, openright]{memoir}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\usepackage{fontspec}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[autostyle=true,french=guillemets,maxlevel=3]{csquotes}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage[protrusion=true]{microtype}


\makeatletter

\renewcommand\@makefnmark{\hbox{\@textsuperscript{\normalfont\color{BurntOrange}\@thefnmark}}}
\renewcommand\@makefntext[1]{%
  \parindent 1em\noindent
            \hb@xt@1.8em{%
                \hss\@textsuperscript{\normalfont\@thefnmark}}#1}
\makeatother

\usepackage[hidelinks, pdfusetitle]{hyperref} % Creates hyperlinks and index in the PDF document, preferably load after biblatex

\begin{document}
\let\svat @
\catcode`@=\active
\def@{\color{red}\svat}


\chapterstyle{bringhurst}

This is a link in a footnote \footnote{My link is \href{http://www.google.fr}{@google} checking revert}

This is a colored link in my text \href{http://www.google.fr}{@google}

This is a normal link \href{http://www.google.fr}{google}

\end{document}

\documentclass{article}
\usepackage{xcolor}
\let\svat @
\catcode`@=\active
\def@{\textcolor{red}{\svat}}
\begin{document}
xy@z
\end{document}

enter image description here

| improve this answer | |
  • 1
    @reyman64 I improved the solution, making it general. – Steven B. Segletes Mar 16 '14 at 1:58

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.