我受到了这个帖子中 OP 的启发: Tikz:绘制铁轨 制作一个 tikzstyle,让我可以绘制任意形状的带黑色边框的银色线条(这实际上是火车轨道的一部分,但链接线程中的示例不符合我的要求)。但是,正如链接线程的评论中所述,双倍距离使线条之间的区域不透明。添加 double=none 只会使它们变成黑色而不是白色……但我需要它们 100% 透明。有没有办法更改我的代码,使其像现在一样工作,但线条之间的空间是透明的?
这是我的最小代码示例:
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{pagecolor}
\definecolor{railColor}{RGB}{200,200,200}
\newcommand{\connectionRailThickness}{0.051cm}
\newcommand{\connectionRailLineThickness}{0.013cm}
\newcommand{\connectionRailDistance}{0.367cm}
\tikzstyle{track}=[
postaction={draw=black,decorate,fill=none,double distance=\connectionRailDistance+\connectionRailThickness-\connectionRailLineThickness,line width=\connectionRailLineThickness},
postaction={draw=railColor,decorate,fill=none,double distance=\connectionRailDistance-\connectionRailThickness-\connectionRailLineThickness,line width=\connectionRailThickness},
postaction={draw=black,decorate,fill=none,double distance=\connectionRailDistance-\connectionRailThickness-\connectionRailLineThickness,line width=\connectionRailLineThickness},
]
\begin{document}
\pagecolor{yellow}
\begin{tikzpicture}
\draw[track] (0,0) to (10,0);
\end{tikzpicture}
\end{document}
答案1
这不完全符合您的要求,但我认为它非常接近。唯一的缺点是您需要\draw
每条路径使用两条线,而不是一条线。第一种使用outlined track
,第二种track
使用。两种样式都使用略微修改的版本Mark Wibrow 的回答。
\begin{tikzpicture}
\draw[outlined track] (0,0) to (10,0);
\draw[track] (0,0) to (10,0);
\begin{scope}[shift=(-45:4)]
\draw [outlined track] (-2,5) to (0,5) to[out= 0,in=270] (2,8);
\draw [track] (-2,5) to (0,5) to[out= 0,in=270] (2,8);
\draw [outlined track] (6,5) to (4,5) to[out=180,in=270] (2,8) to (2,10);
\draw [track] (6,5) to (4,5) to[out=180,in=270] (2,8) to (2,10);
\end{scope}
\end{tikzpicture}
我认为从输出来看结果还不错:
理想情况下,可以实现更多的自动化,但基本想法似乎相当合理。本质上,我们首先用轮廓颜色(black
默认)绘制略微放大的轨道版本,然后用填充颜色(gray
默认)绘制正常大小的轨道版本。这样做之后,我们还可以通过调整装饰代码dash pattern
中使用的定义来包含枕木tracks
。首先,我们调整开关模式以适应较粗的轮廓线和较细的填充线的不同尺寸。然后我们dash phase
以类似的方式适当地抵消它。
这一切都非常简单。代码基本上是马克的——我只是在负责睡眠者的装饰部分添加了几个键、另一种风格和非常小的灵活性。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pagecolor}
% addaswyd o ateb Mark Wibrow: https://tex.stackexchange.com/a/198114/
\usetikzlibrary{decorations,fit,fadings}
% Layers
\pgfdeclarelayer{sleeper}
\pgfdeclarelayer{rail}
\pgfsetlayers{sleeper,rail,main}
\pgfdeclaredecoration{tracks}{final}{%
\state{final}{%
\pgftransformreset% <- I think is possibly vital.% <- cfr: why?
%
% Get bounding box of decorated path as a node.
% Must do it this way using basic layer.
\pgftransformshift{\pgfpointanchor{current path bounding box}{south west}}%
\pgfcoordinate{@1}\pgfpointorigin
\pgftransformshift{\pgfpointanchor{current path bounding box}{north east}}%
\pgfcoordinate{@2}\pgfpointorigin
\node [fit=(@1)(@2), inner sep=\railsep+2*\railwidth] (@@) {};
%
% Create a fading for the track.
\pgfinterruptpicture%
\begin{tikzfadingfrompicture}[name=tracks]
\path[draw=transparent!0, line width=\railsep+2*\railwidth, postaction={draw=transparent!100, line width=\railsep}] \pgfextra{\pgfsetpath\pgfdecoratedpath};
\useasboundingbox (@@.south west) (@@.north east);
\end{tikzfadingfrompicture}%
\endpgfinterruptpicture
%
% Draw sleepers.
\ifx\sleeperlayer\emptylayer\else\pgfonlayer{\sleeperlayer}\fi
\draw [draw=\sleepercolor, line width=\sleeperlength, dash pattern=on \sleeperwidth off {\sleepersep-\sleeperwidth+\sleeperowidth}, dash phase={0.5*(\sleeperwidth-\sleeperowidth)}, every sleeper/.try] \pgfextra{\pgfsetpath\pgfdecoratedpath};
\ifx\sleeperlayer\emptylayer\else\endpgfonlayer\fi
%
% Draw the track
\ifx\raillayer\emptylayer\else\pgfonlayer{\raillayer}\fi
\fill [path fading=tracks, fit fading=false, fading transform={shift=(@@.center)}, fill=\railcolor] (@@.south west) rectangle (@@.north east);
\ifx\raillayer\emptylayer\else\endpgfonlayer\fi
}
}
\def\emptylayer{}
\tikzset{%
track/.style={
decoration=tracks, decorate
},
outlined track/.style={
track,
decorations/.cd,
rail width=\railowidth,
rail sep=\railisep,
rail color=\raildraw,
sleeper color=\sleeperdraw,
sleeper width=\sleeperowidth,
},
decorations/.cd,
rail sep/.store in=\railsep,
rail inner sep/.store in=\railisep,
rail width/.store in=\railwidth,
rail inner width/.forward to=/tikz/decorations/rail width,
rail outer width/.store in=\railowidth,
rail color/.store in=\railcolor,
rail draw/.store in=\raildraw,
rail layer/.store in=\raillayer,
sleeper sep/.store in=\sleepersep,
sleeper width/.store in=\sleeperwidth,
sleeper inner width/.store in=\sleeperiwidth,
sleeper inner width/.forward to=/tikz/decorations/sleeper width,
sleeper outer width/.store in=\sleeperowidth,
sleeper length/.store in=\sleeperlength,
sleeper color/.store in=\sleepercolor,
sleeper draw/.store in=\sleeperdraw,
sleeper layer/.store in=\sleeperlayer,
rail sep=4pt,
rail inner sep=3pt,
rail inner width=1pt,
rail outer width=2pt,
rail color=gray,
rail draw=black,
rail layer=rail,
sleeper sep=6pt,
sleeper inner width=1pt,
sleeper outer width=2pt,
sleeper length=10pt,
sleeper color=gray,
sleeper draw=black,
sleeper layer=sleeper,
}
\begin{document}
\pagecolor{yellow}
\begin{tikzpicture}
\draw[outlined track] (0,0) to (10,0);
\draw[track] (0,0) to (10,0);
\begin{scope}[shift=(-45:4)]
\draw [outlined track] (-2,5) to (0,5) to[out= 0,in=270] (2,8);
\draw [track] (-2,5) to (0,5) to[out= 0,in=270] (2,8);
\draw [outlined track] (6,5) to (4,5) to[out=180,in=270] (2,8) to (2,10);
\draw [track] (6,5) to (4,5) to[out=180,in=270] (2,8) to (2,10);
\end{scope}
\end{tikzpicture}
\end{document}