Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to place a shaded rectangle between the left and right footers, which fills the space in-between. The left footer can be of variable width, so I would like the rectangle to automatically fill however much space is remaining.

The following code demonstrates what I want to do (with the centre footer forming part of the left footer), but the width of the rectangle is fixed. I would like to change the rectangle's width to fill the remaining space automatically.

\documentclass{article}

\usepackage{tikz}
\usepackage[includeheadfoot,margin=0.8cm]{geometry}
\usepackage{fancyhdr}

\fancyhf{}    
\pagestyle{fancy}
\rfoot{1 of 10}

\definecolor{shadeleft}{RGB}{222,63,94}
\definecolor{shaderight}{RGB}{112,67,96}

\lfoot{
This text can be of variable width
\hspace{5pt}
% The value below of 2in should change to fill the remaining space
\tikz\shade[left color=shadeleft,right color=shaderight] (0,0)  rectangle (2in,2pt);
}

\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}

For info, I am actually using this code in a class, which is why the left footer text can change (between different documents).

I am using xelatex, although I don't think that makes any difference.

share|improve this question

1 Answer 1

up vote 17 down vote accepted

If you don't mind putting the variable-width text into a node, then it is relatively easy. You can adjust the spacing to your liking further.

\documentclass{article}

\usepackage{tikz,fancyhdr,lipsum}
\usetikzlibrary{calc}
\usepackage[includeheadfoot,margin=0.8cm]{geometry}

\fancyhf{}    
\pagestyle{fancy}
\rfoot{1 of 10}
\definecolor{shadeleft}{RGB}{222,63,94}
\definecolor{shaderight}{RGB}{112,67,96}

\lfoot{%
  \tikz[baseline=(a.text)]{
  \shade[left color=shadeleft,right color=shaderight] 
       node[outer xsep=0,inner xsep=0,
            append after command={
           let \p1=($(a.east)-(a.west)$),
               \n1={veclen(\x1,\y1)},
               \n2={width(" 1 of 10 ")}
               in 
               ([xshift=5pt]a.south east|-a.text) 
               rectangle 
               ++({(\textwidth-(\n1+\n2+5)*1pt)},2pt)
           }
           ] (a)
       {This text can be of variable width};
  }%
}

\begin{document}
\lipsum[1-15]
\end{document}

enter image description here

share|improve this answer
    
Wow, you're an absolute star, thank you. I spent literally hours on that! Will up-vote once my reputation score is high enough ;-) –  abeverley Mar 31 at 8:50
    
@abeverley My pleasure. –  percusse Mar 31 at 9:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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