使用 Tikz 创建分段函数填空

使用 Tikz 创建分段函数填空

我正在尝试重新创建类似于 Mark Sparks 示例的东西(附图片)。到目前为止,我已经想出了以下内容。但是,我不知道如何制作大括号以及逗号在此处输入图片描述就像我们对分段函数所做的那样。我想知道是否有人可以帮忙?

感谢您的时间,

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{graphics}
\usepackage{graphicx}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}
        

        \path(8,21.25)--(12,21.25)node[midway]{Equation of Each Piece};
                
                \draw[rounded corners](8,21)--(12,21)--(12,20)--(8,20)--cycle;
                
                \begin{scope}[shift={(4.25,0)}]
                    \draw[rounded corners](8,21)--(12,21)--(12,20)--(8,20)--cycle;
                    \path(8,21.75)--(12,21.75)node[midway]{Constraints of};
                    \path(8,21.25)--(12,21.25)node[midway]{Each Piece};
                \end{scope}
                
                \begin{scope}[shift={(4.25,-1.25)}]
                    \draw[rounded corners](8,21)--(12,21)--(12,20)--(8,20)--cycle;
                \end{scope}
                
                \begin{scope}[shift={(0,-1.25)}]
                    \draw[rounded corners](8,21)--(12,21)--(12,20)--(8,20)--cycle;
                \end{scope}
\end{tikzpicture}
\end{document}

答案1

有一种方法可以做到。我编辑了您的代码以使用相对位置和一些预定义的节点样式。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,decorations.pathreplacing}
\tikzset{
myboxl/.style={
draw,rounded corners,minimum height=1cm,minimum width=4cm,
},
myboxr/.style={
draw,rounded corners,minimum height=1cm,minimum width=2cm,
}
}

\begin{document}
\begin{tikzpicture}
\node (fn) {$f(x)=$};
\draw [decorate,decoration=brace] ([shift={(0.2,-2)}]fn.east) -- ++(0,4);
\node (bl) [myboxl,anchor=west] at ([shift={(0.4,-1.5)}]fn.east) {};
\node (ml) [myboxl,above=0.5cm] at (bl.north) {};
\node (tl) [myboxl,above=0.5cm] at (ml.north) {};
\node (ltext) [above,text height=12pt,text depth=5pt] at (tl.north) {Equation of Each Piece};
\node (bc) [right=5pt] at ([yshift=-10pt]bl.east) {\Large\textbf{,}};
\node (mc) [right=5pt] at ([yshift=-10pt]ml.east) {\Large\textbf{,}};
\node (tc) [right=5pt] at ([yshift=-10pt]tl.east) {\Large\textbf{,}};
\node (br) [myboxr,right=0.7cm] at (bl.east) {};
\node (mr) [myboxr,right=0.7cm] at (ml.east) {};
\node (tr) [myboxr,right=0.7cm] at (tl.east) {};
\node (rtext) [above,text height=12pt,text depth=5pt,align=center] at (tr.north) {Constraint of\\Each Piece};
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:有两行

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,decorations.pathreplacing}
\tikzset{
myboxl/.style={
draw,rounded corners,minimum height=1cm,minimum width=4cm,
},
myboxr/.style={
draw,rounded corners,minimum height=1cm,minimum width=2cm,
}
}

\begin{document}
\begin{tikzpicture}
\node (fn) {$f(x)=$};
\draw [decorate,decoration=brace] ([shift={(0.2,-1.2)}]fn.east) -- ++(0,2.4);
\node (bl) [myboxl,anchor=west] at ([shift={(0.4,-0.75)}]fn.east) {};
\node (tl) [myboxl,above=0.5cm] at (bl.north) {};
\node (ltext) [above,text height=12pt,text depth=5pt] at (tl.north) {Equation of Each Piece};
\node (bc) [right=5pt] at ([yshift=-10pt]bl.east) {\Large\textbf{,}};
\node (tc) [right=5pt] at ([yshift=-10pt]tl.east) {\Large\textbf{,}};
\node (br) [myboxr,right=0.7cm] at (bl.east) {};
\node (tr) [myboxr,right=0.7cm] at (tl.east) {};
\node (rtext) [above,text height=12pt,text depth=5pt,align=center] at (tr.north) {Constraint of\\Each Piece};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

你实际上不需要tikz这个。你可以使用cases\framebox

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}

\newcommand{\htm}{\rule{0pt}{4mm}} % adjust 4mm to set height of boxes

\begin{document}

\[\setlength{\fboxrule}{0.6pt}
f(x)=\begin{cases}
  \framebox[4cm]{\raisebox{7mm}[0pt]{\footnotesize Equation of Each Piece}\htm}\ , 
     & \framebox[2cm]{\raisebox{8.45mm}[0pt]{\footnotesize \begin{tabular}{c}Constraint of\\[-1ex] 
     Each Piece\end{tabular}}\htm}\\[1ex]
  \framebox[4cm]{\htm}\ , & \framebox[2cm]{\htm}\\[1ex]
  \framebox[4cm]{\htm}\ , & \framebox[2cm]{\htm}
\end{cases}
\]

\end{document}

答案3

这是我使用 Ti 的建议Zmatrixcalligraphic brace来自 Ti 的装饰Zcalligraphy库:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{decorations.pathreplacing, calligraphy, matrix}

\begin{document}
\begin{tikzpicture}[
  box/.code={%
    \ifnum\pgfmatrixcurrentrow>1
      % Draw the rectangles
      \tikzset{nodes={draw, text height=8.5pt, text depth=3.5pt}}%
    \fi
  }]

  \matrix (M) [
    matrix of nodes, nodes in empty cells,
    nodes={anchor=base, align=center},
    column 1/.style={box, nodes={text width=12em}},
    column 2/.style={nodes={yshift=-0.3ex}}, % lower the commas a little bit
    column 3/.style={box, nodes={text width=6em}},
  ]
  { Equation of Each piece & &
             {\parbox[b]{6em}{\centering Constraint of\\Each Piece}}\\
     & , &\\[0.7ex]
     & , &\\[0.7ex]
     & , &\\
  };

\draw[decorate, decoration={calligraphic brace, mirror, amplitude=0.8ex},
      line width=0.6pt]
  ([xshift=-0.3em] M-2-1.north west) -- node[left] {$f(x) = {}$}
  ([xshift=-0.3em] M-4-1.south west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

对于两行来说,几乎相同(为什么问题中没有说明?):

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{decorations.pathreplacing, calligraphy, matrix}

\begin{document}
\begin{tikzpicture}[
  box/.code={%
    \ifnum\pgfmatrixcurrentrow>1
      % Draw the rectangles
      \tikzset{nodes={draw, text height=8.5pt, text depth=3.5pt}}%
    \fi
  }]

  \matrix (M) [
    matrix of nodes, nodes in empty cells,
    nodes={anchor=base, align=center},
    column 1/.style={box, nodes={text width=12em}},
    column 2/.style={nodes={yshift=-0.3ex}}, % lower the commas a little bit
    column 3/.style={box, nodes={text width=6em}},
  ]
  { Equation of Each piece & &
             {\parbox[b]{6em}{\centering Constraint of\\Each Piece}}\\
     & , &\\[0.7ex]
     & , &\\
  };

\draw[decorate, decoration={calligraphic brace, mirror, amplitude=0.8ex},
      line width=0.6pt]
  ([xshift=-0.3em] M-2-1.north west) -- node[left] {$f(x) = {}$}
  ([xshift=-0.3em] M-3-1.south west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容