How to scale and clip a path at the same time?

Again, i started a question here, and i found the solution. But i think this method maybe useful for others.

The problem: i exported the TikZ code of one path from Inkscape; then, i wanted to use it in a tikzpicture. It's easy, if i put it in one other tikzpicture, scaling with x,y parameters, inside a \node. But i wanted to use the path for clipping, what is inpossible, if the path is inside a \node and tikzpicture, or an other \scope.

The exported path looks like this (for example):

\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}


The question: how to scale and place the path to the expected size and position, and use it for clipping? Especially, how to make text nodes black outside, and white inside the path? I will answer my question as i found the solution, but i'm wondering if anybody knows other solution?

-

(See below: added stuff concerning scaling)

Here is a much simpler way to achieve part of what you are attempting: switching the color of the text over a background shape. I'm not concerned about the scaling of some imported path. This approach has the advantage that the text is only entered once. The idea is to use the environ package.

The code is

\documentclass{standalone}

\usepackage{tikz}

\usepackage{environ}

\NewEnviron{flipflop}{%
\begin{tikzpicture}
\node[anchor=center,inner sep=0pt,text width=7cm] at (0,0) {\BODY};
\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}


The result is

New stuff : The \clip command may be scaled and shifted. For example, if the line containg the \clipcommand is changed to

\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);


then the output is

If the scale is changed to 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);


Then the output is

-
Yes, i know that a simple clip works well, but here i can scale the circle by setting the radius parameter. With such complicated paths, like in my example (what was exported from Inkscape), some other method needed for scaling. Thanks for the idea, the newEnviron makes the code more clear! –  deeenes Mar 7 '12 at 12:15
Wow, it's good to know, that i can pass the scale and shift parameters to \clip command. Thank You for this compact and nice solution! –  deeenes Mar 12 '12 at 17:45

The solution i've used is the following:

-- Place the text node(s) in the tikzpicture in their normal color (now it's black):

\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}
};


-- Start a scope, where you can scale and position the path (originaly exported from Inkscape). Positioning with shift parameter, scaling with x and y parameters:

\scope[shift={(-30,-70)},y=4pt, x=4pt]


-- Place the path once to fill it with custom color:

  \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;


-- Insert the path again, because clip and fill somehow doesn't work together in this case:

  \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;


-- Now the trick: an other \scope inside the first one. But this \scope have exactly the opposite shift parameters, so we return to the original position. x and y should be 10 mm, what i actually don't understand, because the unit size of this tikzpicture was 1 mm.

\scope[shift={(30cm,70cm)},x=10mm,y=10mm]


-- Inside this \scope, i put the same node(s) or whatever tikz objects, what i want to clip, but here the color is white:

\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}
};


-- Finally, close the two scopes:

\endscope
\endscope


The whole file looks like this:

\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{\footskip}{0mm}
\setlength{\textheight}{85mm}
\setlength{\textwidth}{79mm}
\usepackage{tikz}
\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}


I'm satisfied with the result, because the text nodes are exactly in identical position, so the black and white text is perfectly readable.

-
I take you answer with some modifications To obtain a more compact code. –  Alain Matthes Mar 8 '12 at 9:27

Here a possibility. Before to explain my solution. My code is for pdflatex but you can modify to use with xelatex. I need to explain some problems with your code. About geometry you can simplify and remove a lot of lines. About TikZ xcolor and color this is more important because you may have some problems. First TikZ loads xcolor and xcolor loads '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}.

Now about your question and your answer. Parts of the code is used twice. A good thing is to put your text in macro. In some case a box can be useful. Here \newcommand\mytext

Then you place the text in a minipage, but TikZ can do this by itself. With a text node, you need only to specify the width by text width = \textwidth-1em. You can specify the font with font = \fontsize{12}{13}\selectfont}. You use these options twice, for the black and the white text, so it's preferable to save these options in a local style [every node/.style=.

Now the problem is to place correctly different objects. Your method is to complex. First you use [yscale=-0.1,xscale=0.1] but this is useful only for the import shape, then you place the first node at (10,10) but why not at (0,0) (the default). I put this node at (0,0)and I named it (txt) now it's easy to put the white text exactly at the same position.

Now we need to place the import shape (red for me). A good thing that you do, it's to use x=... and y=..... I used x = .4pt, y = -.4pt because I combined 0.1 and -0.1with 4pt and 4pt.

I'm not very happy with shift = {(-4,8)} because I think it's possible to add something automatically but my first idea is complicated. It's the import shape the problem with a bad origin but perhaps I'm wrong.

Here my proposition :

    \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}


-
Thank You! Your answer made a lot of things clear for me, thanks for all the good advices! –  deeenes Mar 12 '12 at 17:41