使用 TikZ 将文本放入形状中

使用 TikZ 将文本放入形状中

解释

我尝试将文本放入形状中以跟随图形的边缘以获得如下效果:

想要的渲染的 mokup

(红色区域仅用于解释)

现在的情况

目前,这是我的 MWE

\documentclass[a5paper, twoside]{article}
% \usepackage[english]{babel}
% \usepackage{blindtext}


\usepackage{geometry}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{shapepar}
\usepackage{microtype}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc,fit,intersections}

\geometry{a5paper}

\def\shapeparnodeaccuracy{2}
\newcommand\shapeparnode[6][]{
  % 6 parameters:
  % style for node (default:empty),
  % h margin, v margin, left path, right path, text (just one paragraph!)

  % name left and right paths and compute there bounding boxes
  \begin{scope}[local bounding box=leftbb]
    \path[name path global=left,xshift=#2] #4;
  \end{scope}
  \node[inner ysep=-#3,inner xsep=0pt,fit=(leftbb)](leftbb){};
  \begin{scope}[local bounding box=rightbb]
    \path[name path global=right,xshift=-#2] #5;
  \end{scope}
  \node[inner ysep=-#3,inner xsep=0pt,fit=(rightbb)](rightbb){};

  % global bounding box
  \path let
  \p1=(leftbb.north west), \p2=(leftbb.south west),
  \p3=(rightbb.north east), \p4=(rightbb.south east)
  in
  \pgfextra{
    \pgfmathsetmacro{\ymin}{(\y1 < \y3) ? \y1 : \y3}
    \pgfmathsetmacro{\ymax}{(\y2 > \y4) ? \y2 : \y4}
    \typeout{ymin \ymin}
    \typeout{ymax \ymax}
  } node[inner sep=0,fit={(\x1,\ymin pt)(\x3,\ymax pt)}](mybb){};

  % compute nb steps
  \path let \p1=(mybb.north), \p2=(mybb.south) in
  \pgfextra{
    \pgfmathsetmacro{\fnthght}{1em/\shapeparnodeaccuracy}
    \pgfmathtruncatemacro{\nbsteps}{(\y1-\y2)/\fnthght}
    \xdef\nbsteps{\nbsteps}
    \typeout{nb steps \nbsteps}
  };

  % horizontal references
  \path (mybb.north) -- (mybb.south)
  \foreach \cnt in {0,1,...,\nbsteps}{
    \pgfextra{\pgfmathsetmacro{\pos}{\cnt/\nbsteps}}
    coordinate[pos=\pos] (ref \cnt)
  };

  % left and right boundaries coordinates
  \foreach \cnt in {0,1,...,\nbsteps}{
    % an horizontal line from left to right
    \path[name path=ltor]
    (mybb.west |- ref \cnt) --  (mybb.east |- ref \cnt);
    % same line from right to left
    \path[name path=rtol]
    (mybb.east |- ref \cnt) -- (mybb.west |- ref \cnt);
    % left boundary
    \path[name intersections={of=rtol and left,by={l \cnt},sort by=rtol}];
    % right boundary
    \path[name intersections={of=ltor and right,by={r \cnt},sort by=ltor}];
  }
  % start point (and initial value of boundshape)
  \path let \p1=(l 0) in 
  \pgfextra{
    \pgfmathsetmacro{\xstart}{\x1}
    \xdef\boundshape{{0}{0}b{\xstart}}
    \xdef\xmin{\xstart}
    \xdef\xmax{\xstart}
  };

  % top and bottom
  \path let \p1=(l 0), \p2=(l \nbsteps) in
  \pgfextra{
    \pgfmathsetmacro{\ystart}{\y1}\xdef\ystart{\ystart}
    \pgfmathsetmacro{\yending}{\y2}\xdef\yending{\yending}
  };
  % incremental definition of boundshape
  \foreach \cnt in {0,1,...,\nbsteps}{
    \path let \p1=(l \cnt), \p2=(r \cnt) in
    \pgfextra{
      \pgfmathsetmacro{\start}{\x1}
      \pgfmathsetmacro{\len}{\x2-\x1}
      \pgfmathsetmacro{\ypos}{\cnt/\nbsteps*(\ystart - \yending)}
      {\let\\=\relax \xdef\boundshape{\boundshape\\{\ypos}t{\start}{\len}}}
      \pgfmathsetmacro{\xmin}{(\xmin < \start) ? \xmin : \start}
      \xdef\xmin{\xmin}
      \pgfmathsetmacro{\xmax}{(\xmax > \start + \len) ? \xmax : \start + \len}
      \xdef\xmax{\xmax}
    };
  }
  % draw the node with text in a shapepar
  \pgfmathsetmacro{\ymax}{\ystart - \yending}
  {\let\\=\relax \xdef\boundshape{\boundshape\\{\ymax}e{0}}}
  \node[#1,text width=\xmax pt - \xmin pt,align=flush left,
  anchor=north west,inner sep=0]
  at (mybb.north west -| \xmin pt,0)
  {\Shapepar[1pt]{\boundshape}#6\par};
}

\begin{document}

\thispagestyle{empty}
\newgeometry{top=1cm, bottom=1cm, inner=1.4cm, outer=0.3cm}

\noindent%
\rlap{
\begin{tikzpicture}[remember picture, overlay]
    \node [shift={(4.5cm, -10.5cm)}] at (current page.north west)
        {\hspace{3cm} \includegraphics{nil.pdf} };
\end{tikzpicture}
}
\begin{tikzpicture}  
  \def\pathone{(0,0) to (0,-20)}
  \def\pathtwo{(4,-4) to [out=up, in=down] (6.5,0)}
  \def\pathtwo{(4,0) -- (-.5,-7) -- (2,-5.2) to[bend right] (3,-10)}%
      
  \shapeparnode{1em}{.2em}{\pathone}{\pathtwo}{%
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
}
  \draw \pathone -- \pathtwo -- cycle;
\end{tikzpicture}

\restoregeometry


\end{document}

nil.pdf可以建立在http://taniere.info/tmp/nil.pdf

但渲染效果……并不像预期的那样。

我得到的渲染

一些尝试

导出至 psrick

使用 Inkscape,我尝试将形状的正确路径转换为 ​​pspicture,得到了以下结果:

%LaTeX with PSTricks extensions
%%Creator: inkscape 0.92.1
%%Please note this file requires PSTricks extensions
\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}
\begin{pspicture}(197.64199565,498.89561931)
{
\newrgbcolor{curcolor}{0 0 0}
\pscustom[linewidth=0.99999995,linecolor=curcolor]
{
\newpath
\moveto(196.44835276,498.86665301)
\curveto(196.44835276,498.86665301)(194.45963339,464.59591915)(194.36749228,447.43989144)
\curveto(194.3137852,437.42771884)(194.66475213,419.19976419)(195.25929071,417.41616734)
\curveto(195.85381039,415.63257049)(198.79564346,387.00461459)(195.85381039,372.23199096)
\curveto(194.04201071,363.13383663)(190.35208063,354.13992167)(184.85502992,346.66722876)
\curveto(180.52437165,340.78012325)(171.14639244,339.09029648)(168.20821039,332.39852955)
\curveto(162.4683326,319.32586183)(163.32663307,302.37097679)(169.69453228,289.59243191)
\curveto(174.18788031,280.57561301)(189.93217134,280.10457049)(193.17844157,270.56753742)
\curveto(194.55695244,266.51766025)(193.87900346,260.27539254)(190.20579024,258.08239726)
\curveto(183.6018822,254.13974529)(175.57399181,263.36878466)(167.91096189,264.0277075)
\curveto(156.80989228,264.98226498)(144.75845291,265.67051695)(134.61732283,261.05507128)
\curveto(118.86245291,253.88470277)(108.67836094,238.00902403)(97.16201575,225.08602561)
\curveto(83.33079685,209.56550907)(74.18154331,190.07066813)(59.40941102,175.442876)
\curveto(50.45336693,166.57429018)(38.49879685,161.23578309)(29.08841575,152.85078781)
\curveto(20.34840945,145.06307128)(9.75821102,137.98605711)(5.01002835,127.28602561)
\curveto(-2.04593386,111.38543978)(1.47239055,92.64937679)(0.84831496,75.26472167)
\curveto(0.40750866,62.9853012)(0.52629921,50.67564451)(1.14576378,38.40393427)
\curveto(1.79262992,25.58268073)(4.71291969,0.05680986)(4.71291969,0.05680986)
}
}
\end{pspicture}

我不知道它是否有任何帮助或者对 tikzpicture 是否无用。

导出到 tikz

我也导出到 tikz 并得到以下路径,但我找不到让形状遵循此路径的方法。

\definecolor{cec0000}{RGB}{236,0,0}

\def \globalscale {1.000000}
\begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-\globalscale, xscale=\globalscale, inner sep=0pt, outer sep=0pt]
\begin{scope}[cm={{1.33333,0.0,0.0,-1.33333,(-10.79605,521.90193)}}]
  \path[draw=cec0000,line join=miter,line cap=butt,even odd rule,line
    width=0.600pt] (8.4720,391.0515) -- (9.8097,16.9441) -- (232.0893,16.9441) ..
    controls (232.0893,16.9441) and (230.5977,42.6471) .. (230.5286,55.5142) ..
    controls (230.4883,63.0233) and (230.7516,76.6942) .. (231.1975,78.0319) ..
    controls (231.6434,79.3696) and (233.8497,100.8406) .. (231.6434,111.9201) ..
    controls (230.2845,118.7437) and (227.5171,125.4891) .. (223.3943,131.0936) ..
    controls (220.1463,135.5090) and (213.1128,136.7764) .. (210.9092,141.7952) ..
    controls (206.6043,151.5997) and (207.2480,164.3158) .. (212.0239,173.8997) ..
    controls (215.3939,180.6624) and (227.2021,181.0156) .. (229.6368,188.1684) ..
    controls (230.6707,191.2058) and (230.1623,195.8875) .. (227.4073,197.5323) ..
    controls (222.4544,200.4893) and (216.4335,193.5675) .. (210.6862,193.0733) ..
    controls (202.3604,192.3574) and (193.3218,191.8412) .. (185.7160,195.3028) ..
    controls (173.8998,200.6806) and (166.2618,212.5873) .. (157.6245,222.2795) ..
    controls (147.2511,233.9200) and (140.3892,248.5411) .. (129.3101,259.5119) ..
    controls (122.5930,266.1633) and (113.6271,270.1672) .. (106.5693,276.4560) ..
    controls (100.0143,282.2968) and (92.0717,287.6045) .. (88.5105,295.6295) ..
    controls (83.2185,307.5550) and (85.8573,321.6070) .. (85.3892,334.6455) ..
    controls (85.0586,343.8551) and (85.1478,353.0873) .. (85.6122,362.2911) ..
    controls (86.0973,371.9071) and (88.2876,391.0515) .. (88.2876,391.0515) --
    (8.4720,391.0515);



\end{scope}

\end{tikzpicture}

结论

如何制作具有特定形状的左下角框?

答案1

我不知道自己在做什么——我从各处复制粘贴了随机代码。这是一些东西。显然,缩放是错误的,但我现在没时间了。

\documentclass{article}
\usepackage{background}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{shapepar}
\usepackage{microtype}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc,fit,intersections}

\def\shapeparnodeaccuracy{2}
\newcommand\shapeparnode[6][]{
  % 6 parameters:
  % style for node (default:empty),
  % h margin, v margin, left path, right path, text (just one paragraph!)

  % name left and right paths and compute there bounding boxes
  \begin{scope}[local bounding box=leftbb]
    \path[name path global=left,xshift=#2] #4;
  \end{scope}
  \node[inner ysep=-#3,inner xsep=0pt,fit=(leftbb)](leftbb){};
  \begin{scope}[local bounding box=rightbb]
    \path[name path global=right,xshift=-#2] #5;
  \end{scope}
  \node[inner ysep=-#3,inner xsep=0pt,fit=(rightbb)](rightbb){};

  % global bounding box
  \path let
  \p1=(leftbb.north west), \p2=(leftbb.south west),
  \p3=(rightbb.north east), \p4=(rightbb.south east)
  in
  \pgfextra{
    \pgfmathsetmacro{\ymin}{(\y1 < \y3) ? \y1 : \y3}
    \pgfmathsetmacro{\ymax}{(\y2 > \y4) ? \y2 : \y4}
    \typeout{ymin \ymin}
    \typeout{ymax \ymax}
  } node[inner sep=0,fit={(\x1,\ymin pt)(\x3,\ymax pt)}](mybb){};

  % compute nb steps
  \path let \p1=(mybb.north), \p2=(mybb.south) in
  \pgfextra{
    \pgfmathsetmacro{\fnthght}{1em/\shapeparnodeaccuracy}
    \pgfmathtruncatemacro{\nbsteps}{(\y1-\y2)/\fnthght}
    \xdef\nbsteps{\nbsteps}
    \typeout{nb steps \nbsteps}
  };

  % horizontal references
  \path (mybb.north) -- (mybb.south)
  \foreach \cnt in {0,1,...,\nbsteps}{
    \pgfextra{\pgfmathsetmacro{\pos}{\cnt/\nbsteps}}
    coordinate[pos=\pos] (ref \cnt)
  };

  % left and right boundaries coordinates
  \foreach \cnt in {0,1,...,\nbsteps}{
    % an horizontal line from left to right
    \path[name path=ltor]
    (mybb.west |- ref \cnt) --  (mybb.east |- ref \cnt);
    % same line from right to left
    \path[name path=rtol]
    (mybb.east |- ref \cnt) -- (mybb.west |- ref \cnt);
    % left boundary
    \path[name intersections={of=rtol and left,by={l \cnt},sort by=rtol}];
    % right boundary
    \path[name intersections={of=ltor and right,by={r \cnt},sort by=ltor}];
  }
  % start point (and initial value of boundshape)
  \path let \p1=(l 0) in 
  \pgfextra{
    \pgfmathsetmacro{\xstart}{\x1}
    \xdef\boundshape{{0}{0}b{\xstart}}
    \xdef\xmin{\xstart}
    \xdef\xmax{\xstart}
  };

  % top and bottom
  \path let \p1=(l 0), \p2=(l \nbsteps) in
  \pgfextra{
    \pgfmathsetmacro{\ystart}{\y1}\xdef\ystart{\ystart}
    \pgfmathsetmacro{\yending}{\y2}\xdef\yending{\yending}
  };
  % incremental definition of boundshape
  \foreach \cnt in {0,1,...,\nbsteps}{
    \path let \p1=(l \cnt), \p2=(r \cnt) in
    \pgfextra{
      \pgfmathsetmacro{\start}{\x1}
      \pgfmathsetmacro{\len}{\x2-\x1}
      \pgfmathsetmacro{\ypos}{\cnt/\nbsteps*(\ystart - \yending)}
      {\let\\=\relax \xdef\boundshape{\boundshape\\{\ypos}t{\start}{\len}}}
      \pgfmathsetmacro{\xmin}{(\xmin < \start) ? \xmin : \start}
      \xdef\xmin{\xmin}
      \pgfmathsetmacro{\xmax}{(\xmax > \start + \len) ? \xmax : \start + \len}
      \xdef\xmax{\xmax}
    };
  }
  % draw the node with text in a shapepar
  \pgfmathsetmacro{\ymax}{\ystart - \yending}
  {\let\\=\relax \xdef\boundshape{\boundshape\\{\ymax}e{0}}}
  \node[#1,text width=\xmax pt - \xmin pt,align=flush left,
  anchor=north west,inner sep=0]
  at (mybb.north west -| \xmin pt,0)
  {\Shapepar[1pt]{\boundshape}#6\par};
}\begin{document}
\backgroundsetup{scale = 1, angle = 0,
   contents = {\includegraphics[]
   {nil.pdf}}}
   \begin{tikzpicture}[yshift=15cm]  
   \draw (0,0) circle (0.1);
   \begin{scope}[yshift=15cm]  
  \def\pathone{(0.00,10.48) -- (0.00,0,00)}
  \def\pathtwo{
   (6.26,0.00) ..
   controls (6.26,0.00) and (6.22,0.72) ..
   (6.21,1.08) ..
   controls (6.21,1.29) and (6.22,1.68) ..
   (6.23,1.71) ..
   controls (6.25,1.75) and (6.31,2.35) ..
   (6.25,2.66) ..
   controls (6.21,2.85) and (6.13,3.04) ..
   (6.02,3.20) ..
   controls (5.92,3.32) and (5.73,3.36) ..
   (5.67,3.50) ..
   controls (5.54,3.77) and (5.56,4.13) ..
   (5.70,4.40) ..
   controls (5.79,4.59) and (6.12,4.60) ..
   (6.19,4.80) ..
   controls (6.22,4.88) and (6.20,5.01) ..
   (6.13,5.06) ..
   controls (5.99,5.14) and (5.82,4.95) ..
   (5.66,4.94) ..
   controls (5.43,4.92) and (5.17,4.90) ..
   (4.96,5.00) ..
   controls (4.63,5.15) and (4.42,5.48) ..
   (4.17,5.75) ..
   controls (3.88,6.08) and (3.69,6.49) ..
   (3.38,6.80) ..
   controls (3.19,6.98) and (2.94,7.09) ..
   (2.74,7.27) ..
   controls (2.56,7.43) and (2.34,7.58) ..
   (2.24,7.81) ..
   controls (2.09,8.14) and (2.16,8.53) ..
   (2.15,8.90) ..
   controls (2.14,9.16) and (2.14,9.42) ..
   (2.16,9.67) ..
   controls (2.17,9.94) and (2.23,10.48) ..
   (2.23,10.48);
  }
  \shapeparnode{1em}{.2em}{\pathone}{\pathtwo}{%
    \lipsum[1-4]
    }
  \draw \pathone -- \pathtwo -- cycle;
 \end{scope}
\end{tikzpicture}
\end{document}

错误缩放的文本节点

相关内容