我有以下代码,需要修改以包含背景颜色,但是当我这样做时文本被隐藏了。你能帮我解决这个问题吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{pics/dbox/.style 2 args={code={%
\pgfmathsetmacro{\w}{max((width("#1")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#1")),%
(width("#2")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#2")))*\pgfkeysvalueof{/tikz/dbox/height}}
\path (-\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2)
node[above right] (bl) {#2}
rectangle
(\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2)
node[below left] (tr) {#1};
\node[pic actions,inner sep=0pt,fit=(bl)(tr),path picture={\path[pic actions]
(path picture bounding box.north west)
-- (path picture bounding box.south east);}]
(\pgfkeysvalueof{/tikz/dbox/name}){};
}},dbox/.cd,height/.initial=2cm,name/.initial=}
\begin{document}
\begin{tikzpicture}
\path pic[draw,fill=black!20] {dbox={abc}{qd0}} (3,0);
\end{tikzpicture}
\end{document}
答案1
在此版本中,您可以根据需要设置左下角(bl
)和右上角( )的样式。tr
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{pics/dbox/.style 2 args={code={%
\pgfmathsetmacro{\w}{max((width("#1")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#1")),%
(width("#2")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#2")))*\pgfkeysvalueof{/tikz/dbox/height}}
\path[/tikz/dbox/bl style] (-\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2) |-
(\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2) -- cycle;
\path[/tikz/dbox/tr style] (-\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2) -|
(\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2) -- cycle;
\path (-\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2)
node[above right] (bl) {#2}
rectangle
(\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2)
node[below left] (tr) {#1};
\node[pic actions,inner sep=0pt,fit=(bl)(tr),path picture={\path[pic actions]
(path picture bounding box.north west)
-- (path picture bounding box.south east);}]
(\pgfkeysvalueof{/tikz/dbox/name}){};
}},dbox/.cd,height/.initial=2cm,name/.initial=,
bl style/.style={},tr style/.style={},
bl/.code={\tikzset{dbox/bl style/.style={#1}}},
tr/.code={\tikzset{dbox/tr style/.style={#1}}}}
\begin{document}
\begin{tikzpicture}
\path
(0,0) pic[draw,dbox/.cd,bl={fill=black!20},tr={fill=black!20}] {dbox={abc}{qd0}}
(3,0) pic[draw,dbox/.cd,bl={fill=blue!20},tr={fill=red!20}] {dbox={abc}{qd0}} ;
\end{tikzpicture}
\end{document}