我正在为文档中的一些 tikz 绘图添加边框。有些只是 tikz 环境中的图像,有些则是完整的绘图。
使用此处现有的问答(最具体)包括带有褪色边框的图像 我使用了初始代码,因为我想要一个恒定的灰色淡入淡出效果,而不是淡出图像(因为它们都有白色背景)我已经得到了我想要的外观,尽管在编译时,我在淡入淡出的边缘得到了一条非常细的灰线。当我注释掉最后的 framenode 行并将开始设置为时,它完全消失了:
\node (image)at (0,0) %[square,draw,line width=3mm]
所以我确信我在框架设置中遗漏了一些我上面链接的答案中不明显的内容。
梅威瑟:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc,fadings}
\usetikzlibrary{shapes.geometric}
\tikzstyle arrowstyle=[scale=1]
\tikzstyle arrowstyle2=[scale=2]
\tikzstyle directed=[postaction={decorate,decoration={markings,
mark=at position .65 with {\arrow[arrowstyle]{stealth}}}}]
\tikzstyle reverse directed=[postaction={decorate,decoration={markings,
mark=at position .65 with {\arrowreversed[arrowstyle]{stealth};}}}]
\tikzstyle directed2=[postaction={decorate,decoration={markings,
mark=at position 1 with {\arrow[arrowstyle2]{stealth}}}}]
\tikzstyle reverse directed2=[postaction={decorate,decoration={markings,
mark=at position 0 with {\arrowreversed[arrowstyle2]{stealth};}}}]
\tikzfading[name=fade l,left color=transparent!100,right color=transparent!0]
\tikzfading[name=fade r,right color=transparent!100,left color=transparent!0]
\tikzfading[name=fade d,bottom color=transparent!100,top color=transparent!0]
\tikzfading[name=fade u,top color=transparent!100,bottom color=transparent!0]
% this "frames" a rectangle node
\newcommand\framenode[2][10pt]{
\fill[white,path fading=fade u] (#2.south west) rectangle ($(#2.south east)+(0, #1)$);
\fill[white,path fading=fade d] (#2.north west) rectangle ($(#2.north east)+(0,-#1)$);
\fill[white,path fading=fade l] (#2.south east) rectangle ($(#2.north east)+(-#1,0)$);
\fill[white,path fading=fade r] (#2.south west) rectangle ($(#2.north west)+( #1,0)$);
}
\begin{document}
\begin{tikzpicture}[square/.style={regular polygon,regular polygon sides=4}]
\node (image)at (0,0) [square,draw,line width=3mm]
{
\begin{tikzpicture}
% define coordinates
\coordinate (O) at (0,0) ;
\coordinate (A) at (0,4) ;
\coordinate (B) at (0,-4) ;
\coordinate (C) at (-4,-4) ;
% media
\node[left] at (-2,-2) {House};
\node[right] at (-1,-2) {h};
\node[] at (1,-5) {d};
\node[] at (-1,2.25) {l};
\node[] at (1.5, -1.25) {\textbf{v}};
% axis
\draw[directed2, dash pattern=on5pt off3pt] (O) -- (-50:2.5) ;
\draw[dash pattern=on5pt off3pt] (-4,0) -- (4,0);
\draw[directed2] (2,3) -- (3,3);
\draw[directed2] (2,3) -- (2,2);
\node[] at (3.5, 3) {\textbf{i}};
\node[] at (2, 1.5) {\textbf{j}};
\draw[directed2] (3,-2) -- (3, -3);
\node[] at (3.5, -2.5) {\textbf{g}};
% lines
\draw[red,ultra thick,reverse directed] (O) -- (130:5.2);
\draw[black, directed2, reverse directed2] (0.25,0.25) -- (-3,4.25);
\draw[black, ultra thick] (O) -- (-90:4);
\draw[black, ultra thick] (C) -- (4,-4);
\draw[black, directed2, reverse directed2] (-0.5,-4) -- (-0.5,0);
\draw[black, directed2, reverse directed2] (0,-4.5) -- (2,-4.5);
\draw[red, ultra thick, directed, dash pattern=on5pt off3pt] plot [smooth] coordinates {(0,0) (0.65,-0.75) (1,-1.35) (2,-4)};
% angles
\draw (-1,0) arc (180:130:1);
\node[] at (335:-1.5) {$\alpha$};
\end{tikzpicture}
};
\framenode[35pt]{image}
\end{tikzpicture}
\end{document}
除了淡入淡出边缘的额外 1pt 边框外,效果正是我想要的 - 我将非常感谢任何建议!
答案1
正如上面的评论,这是一个观众问题。
这似乎是某些查看器中的渲染问题,并且根据环境/缩放是否出现而定。防止它出现的解决方案是展平 PDF。
我使用以下解决方案来实现此目的:https://unix.stackexchange.com/questions/162922/is-there-a-way-to-flatten-a-pdf-image-from-the-command-line
pdf2ps orig.pdf - | ps2pdf - flattened.pdf
答案2
这是一种查看器效果——淡入淡出下方的线在边界处可见,但您可以通过使淡入淡出比必要的稍大来避免它:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc,fadings}
\usetikzlibrary{shapes.geometric}
\tikzfading[name=fade l,left color=transparent!100,right color=transparent!0]
\tikzfading[name=fade r,right color=transparent!100,left color=transparent!0]
\tikzfading[name=fade d,bottom color=transparent!100,top color=transparent!0]
\tikzfading[name=fade u,top color=transparent!100,bottom color=transparent!0]
% this "frames" a rectangle node
\newcommand\framenode[2][10pt]{
\fill[white,path fading=fade u] ([yshift=-1pt]#2.south west) rectangle ($([yshift=-1pt]#2.south east)+(0, #1)$);
\fill[white,path fading=fade d] ([yshift=1pt]#2.north west) rectangle ($([yshift=1pt]#2.north east)+(0,-#1)$);
\fill[white,path fading=fade l] ([xshift=1pt]#2.south east) rectangle ($([xshift=1pt]#2.north east)+(-#1,0)$);
\fill[white,path fading=fade r] ([xshift=-1pt]#2.south west) rectangle ($([xshift=-1pt]#2.north west)+( #1,0)$);
}
\begin{document}
\begin{tikzpicture}[square/.style={regular polygon,regular polygon sides=4}]
\node (image)at (0,0) [square,draw,line width=3mm]
{\rule{6cm}{6cm}};
\framenode[35pt]{image}
\end{tikzpicture}
\end{document}