在 tikz 中绘制信封

在 tikz 中绘制信封

我怎样才能定义和发展像这样的形状

在此处输入图片描述

在 Tikz 中?

如果可能的话,有一个在上三角形内书写的选项就太好了(这样形状就可以拉伸了)

答案1

快速解决方案(某些设置可能会进一步调整):

\documentclass{article}
\usepackage{varwidth}
\usepackage{tikz}
\usetikzlibrary{calc}

\newlength\mylen
\newlength\maxwd
\newcommand\Text{}

\newcommand\Envelope[1]{%
\renewcommand\Text{#1}
\settowidth\maxwd{\Text}
\setlength\mylen{\maxwd}
\multiply\mylen by2
\ifdim\mylen>5cm
  \setlength\mylen{4cm}
\fi
\begin{tikzpicture}[line width=4pt]
\coordinate (A) at (0,0);
\coordinate (B) at (1.41\mylen,-\mylen);
\clip 
  ([xshift=-0.5\pgflinewidth,yshift=0.5\pgflinewidth]A) --   
  ([xshift=0.5\pgflinewidth,yshift=0.5\pgflinewidth]A-|B) -- 
  ([xshift=0.5\pgflinewidth,yshift=-0.5\pgflinewidth]B) -- 
  ([xshift=-0.5\pgflinewidth,yshift=-0.5\pgflinewidth]B-|A) -- 
  ([xshift=-0.5\pgflinewidth,yshift=0.5\pgflinewidth]A);
\draw[fill=gray!20,line cap=rect]
  (A) -- (A-|B) -- (B) -- (B-|A) -- (A); 
\draw 
  (B-|A) -- (0.705\mylen,-.3\mylen) -- (B);
\draw[fill=gray!30,rounded corners=15pt] 
  (A) -- (0.705\mylen,-0.6\mylen) -- (A-|B);
\node[anchor=north] 
  at ($ (A)!0.5!(B|-A) $ ) {\parbox{\mylen}{\centering\Text}};
\draw (A) -- (B|-A);
\end{tikzpicture}
}

\begin{document}

\Envelope{Eyes only}

\Envelope{Some longer text for the example}

\end{document}

在此处输入图片描述

答案2

这是这种形状的定义的开始。

目前不考虑键/pgf/minimum width和的值/pgf/minimum height。代码的(注释掉的)部分有问题,我不知道如何解决。

代码

\documentclass[tikz,convert]{standalone}
\makeatletter
\pgfset{envelope radius/.initial=+10pt,
        envelope angle/.initial=40}
\def\pgfmath@smuggleOne#1\endgroup{%
  \expandafter\endgroup\expandafter#1\expandafter=\the#1\relax}
\pgfdeclareshape{envelope}{%
  \savedanchor\northeast{%
    \pgfmathsetcount\pgfutil@tempcnta{\pgfkeysvalueof{/pgf/envelope angle}}%
    \pgfmathtan@{\the\pgfutil@tempcnta}%
    \let\pgf@sh@envelope@tan\pgfmathresult
    \pgfmathcot@{\the\pgfutil@tempcnta}%
    \let\pgf@sh@envelope@cot\pgfmathresult
%    \pgfmathsin@{\the\pgfutil@tempcnta}%
%    \let\pgf@sh@envelope@cos\pgfmathresult
    \pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}%
    \pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
%    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/minimum width}}%
%    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
    %
    \pgf@x=.5\wd\pgfnodeparttextbox
    \advance\pgf@x by \pgf@xc%
    \pgfutil@tempdima=\pgf@x
    \pgf@y=\ht\pgfnodeparttextbox
    \advance\pgf@y by \dp\pgfnodeparttextbox
    \advance\pgf@y by 2\pgf@yc
    \pgfutil@tempdimb=\pgf@y
    %
    \advance\pgf@x by \pgf@sh@envelope@cot\pgf@y
    \multiply\pgf@x by 2
    %
%    \ifdim\pgf@x<\pgf@xb%
%      \pgf@x=\pgf@xb%
%    \fi%
    %
    \pgf@x=.5\pgf@x%
%    % Excursus: (Re-)Calculate minimum height
%    \begingroup
%      \pgfmathsetlengthmacro\pgf@temp{\pgf@sh@envelope@cos*(\pgfkeysvalueof{/pgf/envelope radius})}%
%      \multiply\pgf@x by \pgf@sh@envelope@tan
%      \advance\pgf@x by \pgf@temp
%      \multiply\pgf@x by 2
%      \ifdim\pgf@yb<\pgf@x
%        \pgf@yb=\pgf@x
%      \fi
%      \pgfmath@smuggleOne\pgf@yb
%    \endgroup
    %
    \advance\pgf@x by.5\wd\pgfnodeparttextbox%
    \advance\pgf@x by\pgf@xa%
    %
    \multiply\pgfutil@tempdima by \pgf@sh@envelope@tan
    \advance\pgf@y by .5\pgfutil@tempdima
    \multiply\pgf@y by 2
    %
%    \ifdim\pgf@y<\pgf@yb%
%      \pgf@y=\pgf@yb%
%    \fi%
    %
    \pgf@y=.25\pgf@y%
    \advance\pgf@y by-.5\dp\pgfnodeparttextbox%
    \advance\pgf@y by.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by\pgf@ya%
  }

  \savedanchor\southwest{%
    \pgfmathsetcount\pgfutil@tempcnta{\pgfkeysvalueof{/pgf/envelope angle}}%
    \pgfmathtan@{\the\pgfutil@tempcnta}%
    \let\pgf@sh@envelope@tan\pgfmathresult
    \pgfmathcot@{\the\pgfutil@tempcnta}%
    \let\pgf@sh@envelope@cot\pgfmathresult
%    \pgfmathsin@{\the\pgfutil@tempcnta}%
%    \let\pgf@sh@envelope@cos\pgfmathresult
    \pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}%
    \pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
%    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/minimum width}}%
%    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
    %
    \pgf@x=.5\wd\pgfnodeparttextbox
    \advance\pgf@x by \pgf@xc%
    \pgfutil@tempdima=\pgf@x
    \pgf@y=\ht\pgfnodeparttextbox
    \advance\pgf@y by \dp\pgfnodeparttextbox
    \advance\pgf@y by 2\pgf@yc
    \pgfutil@tempdimb=\pgf@y
    %
    \advance\pgf@x by \pgf@sh@envelope@cot\pgf@y
    \multiply\pgf@x by 2
    %
%    \ifdim\pgf@x<\pgf@xb%
%      \pgf@x=\pgf@xb%
%    \fi%
    %
    \pgf@x=-.5\pgf@x%
%    % Excursus: (Re-)Calculate minimum height
%    \begingroup
%      \pgfmathsetlengthmacro\pgf@temp{\pgf@sh@envelope@cos*(\pgfkeysvalueof{/pgf/envelope radius})}%
%      \multiply\pgf@x by -\pgf@sh@envelope@tan
%      \advance\pgf@x by \pgf@temp
%      \multiply\pgf@x by 2
%      \ifdim\pgf@yb<\pgf@x
%        \pgf@yb=\pgf@x
%      \fi
%      \pgfmath@smuggleOne\pgf@yb
%    \endgroup
    %
    \advance\pgf@x by.5\wd\pgfnodeparttextbox%
    \advance\pgf@x by-\pgf@xa%
    %
    \multiply\pgfutil@tempdima by \pgf@sh@envelope@tan
    \advance\pgf@y by .5\pgfutil@tempdima
    \multiply\pgf@y by 2
    %
%    \ifdim\pgf@y<\pgf@yb%
%      \pgf@y=\pgf@yb%
%    \fi%
    %
    \pgfutil@tempdima=-.5\pgf@y
    \pgf@y=-.25\pgf@y%
    \advance\pgf@y by-.5\dp\pgfnodeparttextbox%
    \advance\pgf@y by.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by-\pgf@ya%
    \advance\pgf@y by \pgfutil@tempdima
  }
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{text}
  \inheritbackgroundpath[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \beforebackgroundpath{%
    \pgfmathsetcount\pgfutil@tempcnta{\pgfkeysvalueof{/pgf/envelope angle}}%
    \pgfsetcornersarced{\pgfpoint{\pgfkeysvalueof{/pgf/envelope radius}}{\pgfkeysvalueof{/pgf/envelope radius}}}%
    \pgfmathtan@{\the\pgfutil@tempcnta}%
    \let\pgf@sh@envelope@tan\pgfmathresult
    \pgfmathcot@{\the\pgfutil@tempcnta}%
    \let\pgf@sh@envelope@cot\pgfmathresult
    %
    \pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}%
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \pgf@process{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}%
    \pgf@xb=\pgf@x
    \pgf@yb=\pgf@y
    \pgf@xc=.5\pgf@xa
    \advance\[email protected]\pgf@xb
    \pgf@yc=.5\pgf@ya
    \advance\[email protected]\pgf@yb
    %
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}%
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xa}{\pgf@ya}}{\pgfqpoint{-\pgf@xc}{-\pgf@sh@envelope@tan\pgf@xc}}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}%
    %
    \pgfsetcornersarced{\pgfpointorigin}
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}%
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xb}{\pgf@yb}}{\pgfqpoint{\pgf@sh@envelope@cot\pgf@yc}{\pgf@yc}}}
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xa}{\pgf@yb}}{\pgfqpoint{-\pgf@sh@envelope@cot\pgf@yc}{\pgf@yc}}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}%
    \pgfusepath{stroke}%
  }
}
\makeatother
\tikzset{every envelope node/.style={draw}}
\fboxsep=-\fboxrule % only used for a tight \fbox! Don't use in document
\begin{document}
\tikz                \node[envelope] {\fbox{\TeX t}};
\tikz[inner xsep=0pt]\node[envelope] {\fbox{\TeX t}};
\tikz[inner ysep=0pt]\node[envelope] {\fbox{\TeX t}};
\tikz[inner sep=0pt] \node[envelope] {\fbox{\TeX t}};
\end{document}

输出

  • 在此处输入图片描述
  • 在此处输入图片描述
  • 在此处输入图片描述
  • 在此处输入图片描述

相关内容