15

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.

1 Answer 1

19

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

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .