Search code examples
latexxelatex

Redefine \footnote to change color of index in Latex


For my work I shall make it possible to change the color of the footnote index optionally.

I found a way to change the color, but it is not flexible.

\RequirePackage{xcolor}
\definecolor{red}{RGB}{165,30,55}
\renewcommand{\thefootnote}{\textcolor{red}{\arabic{footnote}}}

\begin{document}
a footnote\footnote{lalala}
\end{document}

this works. But this does not:

\renewcommand{\thefootnote}[1]{\textcolor{#1}{\arabic{footnote}}}
\begin{document}
a footnote\footnote[red]{lalala}
\end{document}

I think it is because \footnote already has one optional parameter for the index number. Is there a way to change it?


Solution

  • With a new command, one could do something like this:

    \documentclass{article}
    
    \RequirePackage{xcolor}
    
    \newcommand{\cfootnote}[2][black]{%
        {\color{#1}\footnote{#2}}%
    }
    
    \begin{document}
    a footnote\cfootnote{lalala}
    
    
    a footnote\cfootnote[red]{lalala}
    \end{document}
    

    enter image description here

    Another approach with redefining the footnote:

    \documentclass{scrartcl}
    \usepackage{scrletter}
    \usepackage{xcolor}
    
    \let\oldfootnote\footnote
    \usepackage{xparse}
    \usepackage{etoolbox}
    \RenewDocumentCommand{\footnote}{ O{} m O{black}}{%
        \deffootnotemark{\color{#3}\textsuperscript{\thefootnotemark}}%
        \ifstrempty{#1}{%
            \oldfootnote{#2}%
        }{%
            \oldfootnote[#1]{#2}%
        }
    }
    
    \begin{document}
    
    test\footnote{text}[red]
    
    test\footnote{text}
    
    test\footnote[42]{text}
    
    test\footnote[42]{text}[blue]
    
    \end{document}
    

    enter image description here