我的问题
绘制下图的更优雅的解决方案是什么?
我需要多次重复使用该类型的图表来显示不同概念之间的对称性/对立性(因此需要\newcommand
包装tikzfigure
并使用参数)
我的解决方案过于手动了。
我需要什么
- 一种不太需要手动操作的解决方案,用于绘制对立的三角形。
- 文字(或数学)更和谐地融入三角形
- 能够水平镜像图形
MWE
\documentclass[border =2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\newcommand{\Balance}[3]
{
\def\Leftlbl{#1}
\def\Rightlbl{#2}
\def\Centerlbl{#3}
\coordinate (A) at (0,0.05);
\coordinate (B) at (-0.4,-1);
\coordinate (C) at (0.4,-1);
\coordinate (D) at (-5,0);
\coordinate (E) at (-5,-1.5);
\coordinate (F) at (5,0);
\coordinate (G) at (5,1.5);
\fill[orange!70] (A) -- (B) -- (C);
\draw [draw=orange!50,fill=orange!50, text opacity=1] (D)--(E)--(0,0)--cycle;
\draw [draw=orange!80,fill=orange!80, text opacity=1] (F)--(G)--(0,0)--cycle;
\node[color=white] at (barycentric cs:A=1,B=1,C=1) {\small{\Centerlbl}};
\node[color=red] at (-4.2,-0.6) {\tiny{\Leftlbl}};
\node[color=blue] at (4.2,0.6) {\tiny{\Rightlbl}};
\node[circle,fill=red,opacity=0.5] at (0,0) {};
}
\begin{document}
\begin{tikzpicture}
\Balance{text or math that can be lon or short}{but needs fitting}{1 word}
\end{tikzpicture}
\end{document}
答案1
您可以使用eqparbox
使左节点和右节点宽度相等。然后,三角形可以通过一条穿过原点和左(右)节点右下角(左上角)的线与该节点左(右)边界的垂直线之间的交点来计算。为了使用eqparbox
,您需要为图片提供一个唯一的id
。然后只需说
\pic{balance={d=2cm,
L={$\theta$},
left node/.append style={font=\Huge},
left={fill=cyan!60},
R={$\Gamma^+$},
right node/.append style={font=\Huge},
right={fill=magenta!60},
B={$\Delta^0$},
bottom node/.append style={font=\small},
bottom={fill=orange!80},id=B
}};
获得
三角形根据文本调整大小
通过这张图来解释参数d
和。r
这是带有示例的代码。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds}
\usepackage{eqparbox}
\usetikzlibrary{calc}
\tikzset{pics/balance/.style={code={
\tikzset{balance/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/balance/##1}}%
\path (0,0) coordinate (O)
(-\pv{d},0)
node[below left,balance/left node] (L) {\eqmakebox[\pv{id}][l]{\pv{L}}}
(\pv{d},0) node[above right,balance/right node]
(R) {\eqmakebox[\pv{id}][r]{\pv{R}}}
(0,-\pv{v}) node[below=\pv{v},balance/bottom node] (B){\pv{B}}
let \p1=($(L.north)-(L.south)$),\p2=($(R.north)-(R.south)$),
\n1={max(\y1,\y2)} in
(-\pv{d},-\n1) coordinate (Laux)
(\pv{d},\n1) coordinate (Raux)
;
\begin{scope}[on background layer]
\path[balance/left side]
(O) -|
(intersection of O--Laux and L.south west--L.north west) coordinate (BL)
-- cycle;
\path[balance/right side] (O) -|
(intersection of O--Raux and R.south east--R.north east) coordinate (TR)
-- cycle;
\path[balance/bottom side] (O) --
(intersection of O--B.north east and B.south west--B.south east)
--
(intersection of O--B.north west and B.south west--B.south east)
-- cycle;
\end{scope}
\path[balance/left arrow style] (BL|-O) -- (BL);
\path[balance/right arrow style] (TR|-O) -- (TR);
\path (O) node[balance/c]{};
}},balance/.cd,L/.initial={A},R/.initial={A},B/.initial={$=$},
left side/.style={fill=orange},
left/.code={\tikzset{balance/left side/.style={#1}}},
right side/.style={fill=orange},
right/.code={\tikzset{balance/right side/.style={#1}}},
bottom side/.style={fill=orange},
bottom/.code={\tikzset{balance/bottom side/.style={#1}}},
left arrow style/.style={},
left arrow/.code={\tikzset{balance/left arrow style/.style={#1}}},
right arrow style/.style={},
right arrow/.code={\tikzset{balance/right arrow style/.style={#1}}},
left node/.style={text=red},
right node/.style={text=blue},
bottom node/.style={text=white},
c/.style={circle,inner sep=0.5ex,fill=red,opacity=0.8},
d/.initial=1cm,v/.initial=2em,r/.initial=0.5ex,
id/.initial=A}
\begin{document}
\begin{tikzpicture}
\pic{balance={L={text or math that can be long or short},
left node/.append style={font=\tiny},
left={fill=orange!60},
R={but needs fitting},
right node/.append style={font=\tiny},
B={WOI},
bottom node/.append style={font=\small},
bottom={fill=orange!80},id=A
}};
\end{tikzpicture}
\begin{tikzpicture}
\pic{balance={d=2cm,
L={$\theta$},
left node/.append style={font=\Huge},
left={fill=cyan!60},
R={$\Gamma^+$},
right node/.append style={font=\Huge},
right={fill=magenta!60},
B={$\Delta^0$},
bottom node/.append style={font=\small},
bottom={fill=orange!80},id=B
}};
\end{tikzpicture}
\begin{tikzpicture}
\pic{balance={d=3cm,
L={loss on cash},
left={draw=brown,fill=red!20},
left arrow={draw=red,-stealth,thick},
R={gain in future},
right={draw=brown,fill=blue!20},
right arrow={draw=blue,-stealth,thick},
B={$=$},
bottom={fill=orange!80},id=C
}};
\end{tikzpicture}
\begin{tikzpicture}
\path pic (docu) {balance={d=2cm,r=1em,
L={$\theta$},
left node/.append style={font=\Huge},
left={fill=cyan!60},
R={$\Gamma^+$},
right node/.append style={font=\Huge},
right={fill=magenta!60},
B={$\Delta^0$},
bottom={fill=orange!80},id=D
}};
\draw [|<->|,>=latex] (0,0) -- node[below]{$d$} (2,0);
\draw [|<->|,>=latex] (docuR.south east|-0,0) -- node[below]{$r$} ++ (-1em,0);
\end{tikzpicture}
\end{document}