我正在尝试使用 \pgfdeclarehorizontalshading 来获得适用于 3 个不同位置的渐变。我已阅读文档在这里并且不同颜色应该按照指示的确切宽度放置,这似乎相当简单。不幸的是,在实践中我没有得到这些结果。我还改编了我的代码这里还有另一个答案,但出于某种原因,我无法从这个答案中推断出我的需求。下面是 MWE 和相关代码:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{calc}
\newlength{\mymargin}
\setlength{\mymargin}{0.125in}
\usepackage
[
paperwidth=11in,paperheight=17in,layoutwidth=10.75in,layoutheight=16.75in,
left=\mymargin,right=\mymargin,top=\mymargin,bottom=\mymargin,
bindingoffset=0in,landscape=true,marginparwidth=0in,marginparsep=0in
]{geometry}
\pgfdeclarehorizontalshading{grad}{.5in}{
color(0cm)=(white);
color(2.5cm)=(white);
color(5cm)=(pink);
color(8cm)=(black);
color(10cm)=(black)
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
\fill[shading=grad,shading angle=0] (current page.north west) ++ (1in,-3in) rectangle ++ (15in,-3in);
\end{tikzpicture}
\end{document}
我对此不满意的原因如下:
- 首先,它不会从纯白色变成纯粉色再变成纯黑色,至少在可见的部分不会,而且改变这个矩形的大小似乎也不起作用。
- 我的理解是,不同颜色的位置充当百分比,因为从我读到的内容来看,0 是左边,最后一个值(因为它们是按升序排序的)是最右边的值。然而,情况似乎并非如此,因为当我将单位从厘米更改为时,实际渐变会发生变化,我不明白这一点。
- 第三,我不确定为什么在上面提到的答案中重复使用相同的颜色:是为了在给定的空间中获得纯色吗?因为如果是这样(这似乎很有道理),那么我似乎无法在我的例子中重现此功能,这显而易见……
- 最后,我看到通过调整高度( \pgfdeclarehorizontalshading 命令的第二个参数),我可以让渐变填满整个矩形,但我仍然感到惊讶,为什么渲染是这样的,底部有一个黑带?
因此,我的最终目标是准确了解如何定义 N=3 种或更多种颜色,以便将尺子(以厘米为单位)分成这 N 个段,使其在给定的限度内具有峰值颜色。谢谢!
[编辑1] @Ulrike 建议单位应为 bp,总和为 100bp,这可能是事实。然而,这本身似乎并不能解决问题。以下是一个例子:
\pgfdeclarehorizontalshading{grad}{100bp}{
color(0bp)=(white);
color(25bp)=(white);
color(49bp)=(pink);
color(50bp)=(red);
color(51bp)=(pink);
color(80bp)=(black);
color(100bp)=(black)
}
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
\fill[shading=grad,shading angle=0] (current page.north west) ++ (2cm,-4cm) rectangle ++ (10cm,-1cm);
\end{tikzpicture}
答案1
您应该声明阴影的总大小为 100bp(至少是水平阴影的宽度)。文档中没有明确说明,但描述\pgfshadepath
说现在,阴影名称应为宽度和高度为 100 bp 的阴影如何将阴影放入路径的计算也是基于这一假设。
如果你将宽度值更改为 bp
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{calc}
\newlength{\mymargin}
\setlength{\mymargin}{0.125in}
\usepackage
[
paperwidth=11in,paperheight=17in,layoutwidth=10.75in,layoutheight=16.75in,
left=\mymargin,right=\mymargin,top=\mymargin,bottom=\mymargin,
bindingoffset=0in,landscape=true,marginparwidth=0in,marginparsep=0in
]{geometry}
\pgfdeclarehorizontalshading{grad}{100bp}{
color(0cm)=(white);
color(25bp)=(white);
color(50bp)=(pink);
color(80bp)=(black);
color(100bp)=(black)
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
\fill[shading=grad,shading angle=0] (current page.north west) ++ (1in,-3in) rectangle ++ (15in,-3in);
\end{tikzpicture}
\end{document}
你得到
阴影填充路径的规则意味着您还应注意不要向路径添加“未填充”位。比较这两个输出
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage{calc}
\newlength{\mymargin}
\setlength{\mymargin}{0.125in}
\usepackage
[
paperwidth=11in,paperheight=17in,layoutwidth=10.75in,layoutheight=16.75in,
left=\mymargin,right=\mymargin,top=\mymargin,bottom=\mymargin,
bindingoffset=0in,landscape=true,marginparwidth=0in,marginparsep=0in
]{geometry}
\pgfdeclarehorizontalshading{grad}{100bp}{
color(0bp)=(white);
color(25bp)=(white);
color(49bp)=(pink);
color(50bp)=(red);
color(51bp)=(pink);
color(80bp)=(black);
color(100bp)=(black)
}
\begin{document}
\begin{tikzpicture}[show background rectangle,remember picture,every node/.style={inner sep=0,outer sep=0}]
\fill[shading=grad,shading angle=0] (current page.north west) ++ (1in,-3in) rectangle ++ (15in,-3in);
\end{tikzpicture}
\begin{tikzpicture}[show background rectangle,remember picture,every node/.style={inner sep=0,outer sep=0}]
\fill[shading=grad,shading angle=0] ([xshift=1in,yshift=-6in]current page.north west) rectangle ++ (15in,-3in);
\end{tikzpicture}
\end{document}
答案2
像这样:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{calc}
\newlength{\mymargin}
\setlength{\mymargin}{0.125in}
\usepackage
[
paperwidth=11in,paperheight=17in,layoutwidth=10.75in,layoutheight=16.75in,
left=\mymargin,right=\mymargin,top=\mymargin,bottom=\mymargin,
bindingoffset=0in,landscape=true,marginparwidth=0in,marginparsep=0in
]{geometry}
\pgfdeclarehorizontalshading{grad1}{1in}{
color(0cm)=(white);
color(0.1cm)=(white);
color(0.2cm)=(red);
color(0.3cm)=(red);
color(0.4cm)=(orange);
color(0.5cm)=(orange);
color(0.6cm)=(yellow);
color(0.7cm)=(yellow);
color(0.8cm)=(lime);
color(0.9cm)=(lime);
color(1cm)=(green);
color(1.1cm)=(green);
color(1.2cm)=(blue);
color(1.3cm)=(blue);
color(1.4cm)=(purple);
color(1.5cm)=(purple);
color(1.6cm)=(violet);
color(1.7cm)=(violet);
color(1.8cm)=(black);
color(1.9cm)=(black);
color(2cm)=(black)
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
\fill[shading=grad1,shading angle=0] ([shift={(3in,-5in)}]current page.north west) rectangle +(10in,-1in);
\end{tikzpicture}
\end{document
为了实现评论中所说的控制每个片段的长度,您可以手动执行此操作。但是,每种颜色的过渡看起来都不太好。我现在没有更好的解决方案。
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{calc}
\newlength{\mymargin}
\setlength{\mymargin}{0.125in}
\usepackage
[
paperwidth=11in,paperheight=17in,layoutwidth=10.75in,layoutheight=16.75in,
left=\mymargin,right=\mymargin,top=\mymargin,bottom=\mymargin,
bindingoffset=0in,landscape=true,marginparwidth=0in,marginparsep=0in
]{geometry}
\pgfdeclarehorizontalshading{grad1}{1in}{
color(0cm)=(pink);
color(2cm)=(red)
}
\pgfdeclarehorizontalshading{grad2}{1in}{
color(0cm)=(red);
color(2cm)=(green)
}
\pgfdeclarehorizontalshading{grad3}{1in}{
color(0cm)=(green);
color(2cm)=(lime)
}
\pgfdeclarehorizontalshading{grad4}{1in}{
color(0cm)=(lime);
color(2cm)=(yellow)
}
\pgfdeclarehorizontalshading{grad5}{1in}{
color(0cm)=(yellow);
color(2cm)=(blue)
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
\fill[pink] ([shift={(1in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\shade [shading=grad1,shading angle=0] ([shift={(2in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\fill[red] ([shift={(3in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\shade [shading=grad2,shading angle=0] ([shift={(4in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\fill[green] ([shift={(5in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\shade [shading=grad3,shading angle=0] ([shift={(6in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\fill [lime] ([shift={(7in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\shade [shading=grad4,shading angle=0] ([shift={(8in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\fill [yellow] ([shift={(9in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\shade [shading=grad5,shading angle=0] ([shift={(10in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\fill [blue] ([shift={(11in,-2.5in)}]current page.north west) rectangle +(1in,1in);
\foreach \i in {0,1,...,11}
\draw ([shift={(1in+\i in,-2.5in)}]current page.north west) -- ([shift={(1in+\i in,-3in)}]current page.north west) node[below=4pt,font=\Huge]{\i in};
\end{tikzpicture}
\end{document}