The solution below defines two alternative ways of drawing a coloured gradient in the margin background on a selection of pages:
- Invoke the
\conditionalmarginbg
(which takes no argument) at the beginning of your document and then toggle the margin background on/off in your document by using the \startdrawmarginbg
and \stopdrawmarginbg
macros.
- Invoke
\marginbgpagelist
at the beginning of your document; its only argument is a list of pages on which to draw a margin background. That list uses pgffor
's \foreach
syntax, so you can specify 10,...,20
to draw a margin back on pages 10 to 20.
In the example below, I used \conditionalmarginbg
and, for visualisation, I emphasised (red, bold text) where \startdrawmarginbg
and \stopdrawmarginbg
are invoked.
Notes:
- You'll need to run
pdflatex
no fewer than 3 times to get the correct output.
- I presume you need(ed) this for a book, so I didn't bother making my solution compatible with the
oneside
class option. It works fine with twoside
, though.
References:
\documentclass[twoside]{book}
\usepackage{lipsum} % for filler text
\usepackage[margin=3cm]{geometry} % for setting the dimensions
\usepackage{tikzpagenodes}
\usepackage{background}
\backgroundsetup%
{%
contents = {},%
opacity = 1,%
scale = 1,
}
\usepackage{etoolbox} % defines the \ifnumodd macro
\usepackage{xcolor}
% Let's define some colours for this colour gradient
\definecolor{outercolor}{gray}{.7}
\definecolor{innercolor}{gray}{1}
% a switch for toggling margin background on/off
\newif\ifdrawmarginbg\drawmarginbgfalse
\newcommand\startdrawmarginbg{\drawmarginbgtrue}
\newcommand\stopdrawmarginbg{\drawmarginbgfalse}
% redefinition of \emph, just to show where
% my \startdrawmarginbg and \stopdrawmarginbg are invoked in the example
\renewcommand\emph[1]{\textcolor{red}{\bfseries#1}}
% ----- Two possible strategies -----
% --- \conditionalmarginbg ---
% Invoke the following macro at the beginning of the document and then
% use \startdrawmarginbg and \stopdrawmarginbg to toggle the margin background
% gradient on/off in the document.
\newcommand\conditionalmarginbg%
{%
\AddEverypageHook%
{%
\ifdrawmarginbg%
\thispagemarginbg%
\fi%
}%
}
% --- \marginbgpagelist ---
% Invoke the following macro at the beginning of the document.
% It's only (mandatory) argument is a list (using pgffor's \foreach syntax)
% corresponding to the pages on which a margin background gradient
% should be drawn.
\newcommand\marginbgpagelist[1]%
{%
\AddEverypageHook%
{%
\foreach \p in {#1}%
{%
\ifnum\value{page}=\p%
\thispagemarginbg%
\fi
}%
}%
}
% ----- Auxiliary macros -----
% Auxiliary macro for both \conditionalmarginbg and \marginbgpagelist
\newcommand\thispagemarginbg
{%
\ifnumodd{\value{page}}
{\marginbgevenorodd{1}}
{\marginbgevenorodd{0}}
\BgMaterial
}%
% Auxiliary macro for \thispagemarginbg
\newcommand\marginbgevenorodd[1]%
{%
\ifnum#1=1%
\def\shadeangle{90}
\def\marginrectangle
{%
(current page marginpar area.south west |- current page.south)%
rectangle%
(current page.north east)
}
\else%
\ifnum#1=0%
\def\shadeangle{-90}
\def\marginrectangle
{%
(current page marginpar area.south east |- current page.south)%
rectangle%
(current page.north west)
}
\else%
\errmessage{Incompatible value supplied to \string\marginbgevenorodd}
\fi%
\fi%
\backgroundsetup
{%
angle=0,%
contents=%
{%
\tikz[remember picture,overlay]
\shade
[%
bottom color = outercolor,%
top color = innercolor,%
shading angle = \shadeangle,%
] \marginrectangle;%
}%
}%
}
% Use either...
% \marginbgpagelist{2,3,...,4}
% or...
\conditionalmarginbg
\begin{document}
\lipsum[1-10]
\emph{A margin background gradient is drawn from this page onwards.}
\startdrawmarginbg
\lipsum[21-40]
\emph{No margin background gradient is drawn from this page onwards.}
\stopdrawmarginbg
\lipsum[41-50]
\end{document}
\documentclass[varwidth, margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\usepackage{lipsum}
\newcommand\fadingtext[3][]{%
\begin{tikzfadingfrompicture}[name=fading letter]
\node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
\end{tikzfadingfrompicture}%
\begin{tikzpicture}[baseline=(textnode.base)]
\node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
\shade[path fading=fading letter,#2,fit fading=false]
(textnode.south west) rectangle (textnode.north east);%
\end{tikzpicture}%
}
\begin{document}
\noindent \fadingtext{top color=black, bottom color=white, middle color=gray!50}{
\parbox[b]{\linewidth}{\strut\lipsum[1]}
}
\end{document}
Adapted from this answer to fit the requested black-to-white gradient.
EDIT
By using the basic function style in LaTeX and reasoning outside of the box, you can do the following:
The basic function to give some text to \myfunstuff{Hello world!}
will color the text in blue and will restore any following text to black.
\newcommand{\myfunstuff}[1]{\color{blue}{#1}\color{black}}
Which helps you wrap up the previous answer into:
\newcommand{\funstuff}[1]{\fadingtext{top color=black, bottom color=white, middle color=gray!50}{\parbox[b]{\linewidth}{#1}}}
Now onto the small trick you cannot introduce line breaks if you use the parbox
command. The solution is to add \vspace*{.5cm} \\
to simulate the effect of paragraphs:
\noindent\funstuff{
LaTeX follows the design philosophy of separating presentation from content, so that authors can focus on the content of what they are writing without attending simultaneously to its visual appearance. In preparing a LaTeX document, the author specifies the logical structure using simple, familiar concepts such as chapter, section, table, figure, etc., and lets the LaTeX system worry about the formatting and layout of these structures. It therefore encourages the separation of layout from content while still allowing manual typesetting adjustments where needed. This concept is similar to the mechanism by which many word processors allow styles to be defined globally for an entire document or the use of Cascading Style Sheets to style HTML. The LaTeX system is a markup language that also handles typesetting and rendering.[7] \vspace*{.5cm} \\ LaTeX can be arbitrarily extended by using the underlying macro language to develop custom formats. Such macros are often collected into packages, which are available to address special formatting issues such as complicated mathematical content or graphics. Indeed, in the example below, the align environment is provided by the amsmath package. \vspace*{.5cm} \\ In order to create a document in LaTeX, you first write a file, say document.tex, using your preferred text editor. Then you give your document.tex file as input to the TeX program (with the LaTeX macros loaded), and TeX writes out a file suitable for viewing onscreen or printing.[8] This write-format-preview cycle is one of the chief ways in which working with LaTeX differs from what-you-see-is-what-you-get word-processing. It is similar to the code-compile-execute cycle familiar to computer programmers. Today, many LaTeX-aware editing programs make this cycle a simple matter of pressing a single key, while showing the output preview on the screen beside the input window. Some online LaTeX editors automatically refresh the preview.[9][10][11][12] Other online tools provide incremental editing in-place, mixed in with the preview in a streamlined single window.[13]
}
If I use lipsum commands LaTeX will complain that there are no lines to break. So I took the following text from wikipedia and voila!
Best Answer
With
{NiceTabular}
ofnicematrix
. That package provides environment similar to the classical environments{tabular}
and{tabularx}
but which creates PGF/Tikz nodes under the cells, rows and columsn. It's possible to use those nodes to draw with Tikz whatever rule you want after the construction of the array (in the\CodeAfter
).You need several compilations (because
nicematrix
uses PGF/Tikz nodes under the hood).In fact, with
nicematrix
, it's also possible to write a command\MyHrule
to use at the beginning of a row in the{NiceTabular}
. That command will write in the\CodeAfter
the correct instructions to draw the rule as wanted.Moreover, with that programmation, vertical space is reserved for the rules (as does the classical
\hline
ofarray
).