再次,我在这里提出了一个问题,并找到了解决方案。但我认为这种方法可能对其他人有用。
问题:我从 Inkscape 导出了一条路径的 TikZ 代码;然后,我想在 中使用它tikzpicture
。如果我把它放在 中tikzpicture
,使用x,y
参数缩放,在 中,这很容易\node
。但我想使用路径进行剪辑,如果路径在\node
和tikzpicture
或其他中,这是不可能的\scope
。
导出的路径如下所示(例如):
\begin{tikzpicture}[y=0.80pt, x=0.8pt,yscale=-1, inner sep=0pt, outer sep=0pt]
\begin{scope}[shift={(-80.842417,-451.55478)}]% layer1
% path9658
\path[draw=black,fill=black,miter limit=4.00,nonzero rule,line width=2.400pt]
(176.9112,753.3085) .. controls (175.1937,738.0045) and (80.1970,487.6617) ..
(82.4016,472.4964) .. controls (85.5629,452.1139) and (188.0374,667.3609) ..
(196.4229,648.5539) .. controls (204.3755,630.7243) and (242.5487,465.7141) ..
(258.1298,454.3997) .. controls (285.7640,437.3259) and (291.8933,586.8805) ..
(323.4463,583.2517) .. controls (380.2399,578.2668) and (378.0147,580.5031) ..
(434.9882,579.8586) .. controls (450.4961,579.6976) and (387.4331,461.0306) ..
(402.9416,460.9527) -- (556.6130,579.1701) -- (556.6130,579.1701) .. controls
(540.5924,579.1263) and (524.5716,579.1241) .. (508.5512,579.2478) .. controls
(451.6441,579.7674) and (548.2176,721.0517) .. (491.4945,726.0884) .. controls
(460.5044,729.6884) and (275.0470,592.3333) .. (247.7567,608.7835) .. controls
(232.2688,619.7789) and (435.5657,765.3166) .. (427.5294,782.8032) .. controls
(419.1556,801.4606) and (197.8962,687.4329) .. (194.9239,707.7216) .. controls
(192.8788,722.7147) and (191.3417,737.9821) .. (193.8339,753.0290) --
(176.9112,753.3085) -- cycle;
\end{scope}
\end{tikzpicture}
问题:如何缩放路径并将其放置到预期的大小和位置,并将其用于剪辑?特别是,如何使文本节点在路径外部为黑色,在路径内部为白色?我找到解决方案后会回答我的问题,但我想知道是否有人知道其他解决方案?
答案1
(见下文:有关缩放的附加内容)
这里有一个更简单的方法来实现您尝试的部分内容:在背景形状上切换文本的颜色。我不关心某些导入路径的缩放。这种方法的优点是文本只需输入一次。这个想法是使用包environ
。
代码如下
\documentclass{standalone}
\usepackage{tikz}
\usepackage{environ}
\NewEnviron{flipflop}{%
\begin{tikzpicture}
\node[anchor=center,inner sep=0pt,text width=7cm] at (0,0) {\BODY};
{\clip (0,0) circle[radius=2];
\node[anchor=center,fill=black,inner sep=0pt,text width=7cm] at (0,0) {\color{white}\BODY};
}
\end{tikzpicture}}
\begin{document}
\begin{flipflop}
One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. ``What's happened to me?'' he thought.
\end{flipflop}
\end{document}
结果是
新内容:\clip
命令可以缩放和移动。例如,如果包含`\clip``命令的行更改为
\clip[shift={(0,-2)},scale=2] (0,0) to[out=0,in=225] (1,1) to[out=45,in=270] (0.5,2) to[out=90,in=180] (-1,1) to[out=0,in=0] (0,0);
那么输出是
如果比例改为 1:
\clip[shift={(0,-2)},scale=1] (0,0) to[out=0,in=225] (1,1) to[out=45,in=270] (0.5,2) to[out=90,in=180] (-1,1) to[out=0,in=0] (0,0);
然后输出是
答案2
我使用的解决方案如下:
-- 将文本节点放置在tikzpicture
其正常颜色中(现在为黑色):
\begin{tikzpicture}[yscale=-0.1,xscale=0.1]
\node[anchor=center] at (10,10) {
\begin{minipage}{7cm}
\begin{raggedright}
\fontsize{12}{13}\selectfont
One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. ``What's happened to me?'' he thought.\par
\end{raggedright}
\end{minipage}
};
-- 启动一个范围,您可以在其中缩放和定位路径(最初从 Inkscape 导出)。使用shift
参数定位,使用x
和y
参数缩放:
\scope[shift={(-30,-70)},y=4pt, x=4pt]
-- 放置路径一次以用自定义颜色填充:
\fill[black]
(176.9112,753.3085) .. controls (175.1937,738.0045) and (80.1970,487.6617) ..
(82.4016,472.4964) .. controls (85.5629,452.1139) and (188.0374,667.3609) ..
(196.4229,648.5539) .. controls (204.3755,630.7243) and (242.5487,465.7141) ..
(258.1298,454.3997) .. controls (285.7640,437.3259) and (291.8933,586.8805) ..
(323.4463,583.2517) .. controls (380.2399,578.2668) and (378.0147,580.5031) ..
(434.9882,579.8586) .. controls (450.4961,579.6976) and (387.4331,461.0306) ..
(402.9416,460.9527) -- (556.6130,579.1701) -- (556.6130,579.1701) .. controls
(540.5924,579.1263) and (524.5716,579.1241) .. (508.5512,579.2478) .. controls
(451.6441,579.7674) and (548.2176,721.0517) .. (491.4945,726.0884) .. controls
(460.5044,729.6884) and (275.0470,592.3333) .. (247.7567,608.7835) .. controls
(232.2688,619.7789) and (435.5657,765.3166) .. (427.5294,782.8032) .. controls
(419.1556,801.4606) and (197.8962,687.4329) .. (194.9239,707.7216) .. controls
(192.8788,722.7147) and (191.3417,737.9821) .. (193.8339,753.0290) --
(176.9112,753.3085) -- cycle;
-- 再次插入路径,因为在这种情况下clip
和fill
无法一起工作:
\clip
(176.9112,753.3085) .. controls (175.1937,738.0045) and (80.1970,487.6617) ..
(82.4016,472.4964) .. controls (85.5629,452.1139) and (188.0374,667.3609) ..
(196.4229,648.5539) .. controls (204.3755,630.7243) and (242.5487,465.7141) ..
(258.1298,454.3997) .. controls (285.7640,437.3259) and (291.8933,586.8805) ..
(323.4463,583.2517) .. controls (380.2399,578.2668) and (378.0147,580.5031) ..
(434.9882,579.8586) .. controls (450.4961,579.6976) and (387.4331,461.0306) ..
(402.9416,460.9527) -- (556.6130,579.1701) -- (556.6130,579.1701) .. controls
(540.5924,579.1263) and (524.5716,579.1241) .. (508.5512,579.2478) .. controls
(451.6441,579.7674) and (548.2176,721.0517) .. (491.4945,726.0884) .. controls
(460.5044,729.6884) and (275.0470,592.3333) .. (247.7567,608.7835) .. controls
(232.2688,619.7789) and (435.5657,765.3166) .. (427.5294,782.8032) .. controls
(419.1556,801.4606) and (197.8962,687.4329) .. (194.9239,707.7216) .. controls
(192.8788,722.7147) and (191.3417,737.9821) .. (193.8339,753.0290) --
(176.9112,753.3085) -- cycle;
-- 现在来谈谈诀窍:\scope
在第一个里面再放一个。但是这个\scope
有完全相反的shift
参数,所以我们回到原来的位置。x
应该y
是 10 毫米,我其实不明白,因为这个 tikzpicture 的单位大小是 1 毫米。
\scope[shift={(30cm,70cm)},x=10mm,y=10mm]
-- 在这里面\scope
,我放置了相同的节点或任何 tikz 对象,我想要剪辑,但这里的颜色是白色:
\node[anchor=center] at (10,10) {
\begin{minipage}{7cm}
\begin{raggedright}
\fontsize{12}{13}\selectfont\color{white}
One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. ``What's happened to me?'' he thought.\par
\end{raggedright}
\end{minipage}
};
-- 最后,关闭两个范围:
\endscope
\endscope
整个文件如下所示:
\documentclass{minimal}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}
\usepackage{graphicx}
\usepackage[paperwidth=79mm,paperheight=85mm,margin=0cm,left=0cm,bottom=0mm]{geometry}
\usepackage{fullpage}
\setlength{\marginparwidth}{0mm}
\setlength{\marginparsep}{0mm}
\setlength{\hoffset}{-1in}
\setlength{\voffset}{-1in}
\setlength{\topmargin}{0mm}
\setlength{\headheight}{0mm}
\setlength{\headsep}{0mm}
\setlength{\footskip}{0mm}
\setlength{\textheight}{85mm}
\setlength{\textwidth}{79mm}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,calc,svg.path}
\usepackage{xcolor,color}
\tikzstyle{default}=[anchor=north west,text width=79mm,inner sep=0pt,text height=85mm]
\tikzset{grid/.style={gray,very thin,opacity=1}}
\begin{document}
\pagestyle{empty}
\setlength{\parindent}{0em}
\begin{tikzpicture}[yscale=-0.1,xscale=0.1]
\node[anchor=center] at (10,10) {
\begin{minipage}{7cm}
\begin{raggedright}
\fontsize{12}{13}\selectfont
One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. ``What's happened to me?'' he thought.\par
\end{raggedright}
\end{minipage}
};
\scope[shift={(-30,-70)},y=4pt, x=4pt]
\fill[black]
(176.9112,753.3085) .. controls (175.1937,738.0045) and (80.1970,487.6617) ..
(82.4016,472.4964) .. controls (85.5629,452.1139) and (188.0374,667.3609) ..
(196.4229,648.5539) .. controls (204.3755,630.7243) and (242.5487,465.7141) ..
(258.1298,454.3997) .. controls (285.7640,437.3259) and (291.8933,586.8805) ..
(323.4463,583.2517) .. controls (380.2399,578.2668) and (378.0147,580.5031) ..
(434.9882,579.8586) .. controls (450.4961,579.6976) and (387.4331,461.0306) ..
(402.9416,460.9527) -- (556.6130,579.1701) -- (556.6130,579.1701) .. controls
(540.5924,579.1263) and (524.5716,579.1241) .. (508.5512,579.2478) .. controls
(451.6441,579.7674) and (548.2176,721.0517) .. (491.4945,726.0884) .. controls
(460.5044,729.6884) and (275.0470,592.3333) .. (247.7567,608.7835) .. controls
(232.2688,619.7789) and (435.5657,765.3166) .. (427.5294,782.8032) .. controls
(419.1556,801.4606) and (197.8962,687.4329) .. (194.9239,707.7216) .. controls
(192.8788,722.7147) and (191.3417,737.9821) .. (193.8339,753.0290) --
(176.9112,753.3085) -- cycle;
\clip
(176.9112,753.3085) .. controls (175.1937,738.0045) and (80.1970,487.6617) ..
(82.4016,472.4964) .. controls (85.5629,452.1139) and (188.0374,667.3609) ..
(196.4229,648.5539) .. controls (204.3755,630.7243) and (242.5487,465.7141) ..
(258.1298,454.3997) .. controls (285.7640,437.3259) and (291.8933,586.8805) ..
(323.4463,583.2517) .. controls (380.2399,578.2668) and (378.0147,580.5031) ..
(434.9882,579.8586) .. controls (450.4961,579.6976) and (387.4331,461.0306) ..
(402.9416,460.9527) -- (556.6130,579.1701) -- (556.6130,579.1701) .. controls
(540.5924,579.1263) and (524.5716,579.1241) .. (508.5512,579.2478) .. controls
(451.6441,579.7674) and (548.2176,721.0517) .. (491.4945,726.0884) .. controls
(460.5044,729.6884) and (275.0470,592.3333) .. (247.7567,608.7835) .. controls
(232.2688,619.7789) and (435.5657,765.3166) .. (427.5294,782.8032) .. controls
(419.1556,801.4606) and (197.8962,687.4329) .. (194.9239,707.7216) .. controls
(192.8788,722.7147) and (191.3417,737.9821) .. (193.8339,753.0290) --
(176.9112,753.3085) -- cycle;
\scope[shift={(30cm,70cm)},x=10mm,y=10mm]
\node[anchor=center] at (10,10) {
\begin{minipage}{7cm}
\begin{raggedright}
\fontsize{12}{13}\selectfont\color{white}
One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. ``What's happened to me?'' he thought.\par
\end{raggedright}
\end{minipage}
};
\endscope
\endscope
\end{tikzpicture}
\end{document}
我对结果很满意,因为文本节点恰好处于相同的位置,所以黑白文本完全可读。
答案3
这里有一种可能性。在解释我的解决方案之前。我的代码是为 pdflatex 编写的,但您可以修改它以与 xelatex 一起使用。我需要解释一下您的代码的一些问题。关于geometry
您可以简化和删除很多行。关于这TikZ
xcolor
一点color
更重要,因为您可能会遇到一些问题。首先TikZ
加载xcolor
并xcolor
加载“color . In you case,
\usepackage{tikz} is enough but if you want to use some defined colors the best way is to use
\PassOptionsToPackage{dvipsnames,svgnames}{xcolor} before
\usepackage{tikz}”。
现在谈谈你的问题和答案。部分代码使用了两次。最好将文本放入宏中。在某些情况下,框可能会有用。这里\newcommand\mytext
然后,您将文本放置在 中minipage
,但TikZ
可以自行执行此操作。使用文本节点,您只需通过 指定宽度text width = \textwidth-1em
。您可以使用 指定字体font = \fontsize{12}{13}\selectfont}
。您会两次使用这些选项,分别用于黑色和白色文本,因此最好将这些选项保存在本地样式 中[every node/.style=
。
现在的问题是如何正确放置不同的对象。您的方法太复杂了。首先您使用, [yscale=-0.1,xscale=0.1]
但这仅适用于导入形状,然后将第一个节点放置在(10,10)
但为什么不放置在(0,0)
(默认)。我把这个节点放在,(0,0)
并将其命名为(txt),现在很容易将白色文本放在完全相同的位置。
现在我们需要放置导入形状(对我来说是红色)。你做的一件好事是使用x=...
和y=...
..我使用是x = .4pt, y = -.4pt
因为我结合0.1 and -0.1
了4pt and 4pt
。
我对此不太满意,shift = {(-4,8)}
因为我认为可以自动添加一些东西,但我的第一个想法很复杂。这是导入形状的问题,来源不好,但也许我错了。
这是我的建议:
\documentclass{minimal}
\usepackage[paperwidth=79mm,
paperheight=85mm,
margin=0cm,
left=0cm,
bottom=0mm]{geometry}
%\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\setlength{\parindent}{0em}
\newcommand\mytext{One morning, when Gregor Samsa woke from troubled dreams, he found
himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and
if he lifted his head a little he could see his brown belly, slightly domed and divided by
arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide
off any moment. His many legs, pitifully thin compared with the size of the rest of him,
waved about helplessly as he looked. ``What's happened to me?'' he thought.}
\begin{tikzpicture}[every node/.style={text width = \textwidth-1em,
font = \fontsize{12}{13}\selectfont}]
\node (txt) {\mytext};
\begin{scope}[shift = {(-4,8)},
x = .4pt,
y = -.4pt,
fill = red]
\fill[clip]
(176.9112,753.3085) .. controls (175.1937,738.0045) and (80.1970,487.6617) ..
(82.4016,472.4964) .. controls (85.5629,452.1139) and (188.0374,667.3609) ..
(196.4229,648.5539) .. controls (204.3755,630.7243) and (242.5487,465.7141) ..
(258.1298,454.3997) .. controls (285.7640,437.3259) and (291.8933,586.8805) ..
(323.4463,583.2517) .. controls (380.2399,578.2668) and (378.0147,580.5031) ..
(434.9882,579.8586) .. controls (450.4961,579.6976) and (387.4331,461.0306) ..
(402.9416,460.9527) -- (556.6130,579.1701) -- (556.6130,579.1701) .. controls
(540.5924,579.1263) and (524.5716,579.1241) .. (508.5512,579.2478) .. controls
(451.6441,579.7674) and (548.2176,721.0517) .. (491.4945,726.0884) .. controls
(460.5044,729.6884) and (275.0470,592.3333) .. (247.7567,608.7835) .. controls
(232.2688,619.7789) and (435.5657,765.3166) .. (427.5294,782.8032) .. controls
(419.1556,801.4606) and (197.8962,687.4329) .. (194.9239,707.7216) .. controls
(192.8788,722.7147) and (191.3417,737.9821) .. (193.8339,753.0290) --
(176.9112,753.3085) -- cycle;
\node[white] at (txt) {\mytext};
\end{scope}
\end{tikzpicture}
\end{document}