4

I'm using \fcolorbox (from xcolor) within \fancypage (from fancybox) to get a coloured page border. Unfortunately the first page does not show a border, even though the box itself is drawn. How can I get this MWE to work (that is to show the red border on the first page)?

\documentclass{article}
\usepackage{fancybox, xcolor, lipsum}

\fancypage{}{\fcolorbox{red}{yellow}}

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

Note that using \fancypage{}{\fbox} shows the border on the first page as expected.

6
  • Please don't use minimal for examples.
    – cfr
    Oct 14, 2016 at 22:50
  • It is a problem with xcolor or xcolor's interaction with fancypage, it seems.
    – cfr
    Oct 14, 2016 at 23:13
  • fancybox replaces the output routine of LaTeX2e with a modified version of the old one in LaTeX 2.09. Avoid it.
    – egreg
    Oct 14, 2016 at 23:35
  • @egreg what is a good alternative to fancybox in this case (i.e. to draw a frame around the text in each page)?
    – ctef
    Oct 14, 2016 at 23:39
  • 1
    @ctef Nowadays I'd say TikZ.
    – egreg
    Oct 14, 2016 at 23:42

3 Answers 3

3

Here's an alternative using TikZ and the everypage package. The idea is based on code in the background package. tikzpagenodes is used to get easy access to coordinates corresponding to the various parts of the page.

One advantage is that you may, if you wish, create much fancier frames and backdrops.

This example draws one background which includes the text block, header and footer (but not the marginal notes) and another overlaying it which includes just the text block. I've used shadings just to give an idea of the simpler possibilities.

fancy backgrounds in blue

\documentclass{article}
\usepackage{geometry}
\usepackage{everypage}
\usepackage{xcolor}
\usepackage{tikzpagenodes}
\usetikzlibrary{fit}
\usepackage{lipsum}
\AddEverypageHook{%
  \tikz[overlay, remember picture]{% based on background.sty
    \node [top color=blue!50!cyan, bottom color=blue!50!cyan, middle color=blue!25!cyan, draw=blue!75!cyan, line width=3.5pt, inner ysep=15pt, inner xsep=25pt, fit=(current page footer area.south west) (current page header area.north east)] {};
    \node [top color=blue!25!cyan, bottom color=blue!25!cyan, middle color=white, draw=blue!50!cyan, line width=2.5pt, inner sep=5pt, fit=(current page text area.south west) (current page text area.north east)] {};
  }%
}
\begin{document}
\lipsum[1-25]
\end{document}
2

The tcolorbox package with the breakable library allow you to make colored boxes and frames that allow page breaks, among many other well-documented options.

mwe

\documentclass{article}
\usepackage[margin=1.5cm,paperheight=9cm,]{geometry}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbset{breakable}
\begin{document}
\begin{tcolorbox}[
colback=blue!20!, colframe=red!75!black,
boxsep=3mm,arc=0mm,boxrule=2pt]
\lipsum[1-50] % long bla bla bla
\end{tcolorbox}
\end{document}

Edit: I you want headers/footers inside the box, one solution could be one unbreakable tcolobox using the background package:

mwe2

\documentclass{article}
\usepackage{tcolorbox,lipsum}
\usepackage{background}
\backgroundsetup{contents={
\begin{tcolorbox}[colback=blue!10!, 
colframe=red!75!black, boxsep=3mm,
arc=0mm,boxrule=2pt]% 
\vspace{\textheight} 
\end{tcolorbox}
},angle=0,scale=1.2}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{\LaTeX\ examples}
\lhead{Background boxes}
\rfoot{Page \thepage}
\begin{document}
\lipsum[1-25]
\end{document}
0
0

EDIT: My first answer was an emulation of \fancypage{\fcolorbox{red}{yellow}}{}. See this solution below. However, the question was about \fancypage{}{\fcolorbox{red}{yellow}}. So first here comes my solution for that.

1. Solution for \fancypage{}{\fcolorbox{red}{yellow}}

Here is a lightweight solution (no additional packages needed):

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}

%\fancypage{}{\fcolorbox{red}{yellow}}
\newcommand{\xcoord}{1in+\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi-\fboxrule-\fboxsep}
\newcommand{\ycoord}{-1in-\voffset-\textheight-\topmargin-\headheight-\headsep-\footskip-\fboxsep}
\newlength\boxheight
\setlength\boxheight{\dimexpr\textheight+\footskip+\headheight+\headsep+\fboxsep}
\AddToHook{shipout/background}{%
  \put(\xcoord,\ycoord){%
    \fcolorbox{red}{yellow}{\vbox to \boxheight{\hspace{\textwidth}}}
  }%
}%

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

If you have an older LaTeX that doesn't support the shipout/background hook, this can be emulated with the everypage package:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{everypage}

%\fancypage{}{\fcolorbox{red}{yellow}}
\newcommand{\xcoord}{\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi-\fboxrule-\fboxsep}
\newcommand{\ycoord}{-\voffset-\textheight-\topmargin-\headheight-\headsep-\footskip-\fboxsep}
\newlength\boxheight
\setlength\boxheight{\dimexpr\textheight+\footskip+\headheight+\headsep+\fboxsep}
\AddEverypageHook{%
  \put(\xcoord,\ycoord){%
    \fcolorbox{red}{yellow}{\vbox to \boxheight{\hspace{\textwidth}}}
  }%
}%

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

2. Solution for \fancypage{\fcolorbox{red}{yellow}}{}

Here is a lightweight solution (no additional packages needed):

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}

%\fancypage{\fcolorbox{red}{yellow}}{}
\newcommand{\xcoord}{1in+\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi-\fboxrule-\fboxsep}
\newcommand{\ycoord}{-1in-\voffset-\textheight-\topmargin-\headheight-\headsep-\fboxsep}
\AddToHook{shipout/background}{%
  \put(\xcoord,\ycoord){%
    \fcolorbox{red}{yellow}{\vbox to \textheight{\hspace{\textwidth}}}
  }%
}%

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

If you have an older LaTeX that doesn't support the shipout/background hook, this can be emulated with the everypage package:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{everypage}

%\fancypage{\fcolorbox{red}{yellow}}{}
\newcommand{\xcoord}{\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi-\fboxrule-\fboxsep}
\newcommand{\ycoord}{-\voffset-\textheight-\topmargin-\headheight-\headsep-\fboxsep}
\AddEverypageHook{%
  \put(\xcoord,\ycoord){%
    \fcolorbox{red}{yellow}{\vbox to \textheight{\hspace{\textwidth}}}
  }%
}%

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

You must log in to answer this question.

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