我喜欢通过 TikZ 创建文本框。我首先使用那灵活示例。是的 :) 我确实创建了文本框。就在这儿。
但我还有二问题..
问题 1
我用来fit
在文本框左侧创建浅灰色矩形(参考)。但\node(colm)
和\node(colm2)
略大于\node(BOXCONTENT)
。为什么?有什么想法吗?
第二季度
此外,由于我对如何退出环境的了解甚少,我添加了第二个矩形,以便将文本移到节点内的右侧。mybox
有没有更好的方法?(我相信有)我尝试使用类似的东西\setlength{\leftmargini}{2pt}
,但我仍然对部分感到困惑
... (BOXCONTENT) {}\bgroup\rule{0pt}{3ex} }{% \egroup; ...
不起作用!以下平均能量损失重现了我的结果。
\documentclass[12pt]{standalone}
\usepackage{tikz}
\thispagestyle{empty}
\usetikzlibrary{positioning,fit}
%Define colors
\definecolor{Ccolm}{gray}{0.6}
\definecolor{Cfill}{gray}{0.8}
\newenvironment{mybox}[2][]{%
\begin{tikzpicture}[#1]%
%Get the text
\node [inner sep=1pt,text width=#2,fill=Ccolm,]
(BOXCONTENT) {}\bgroup\rule{0pt}{3ex}
}{%
\egroup;
%Why they are slighlty bigger than their fit!!!
\coordinate [left=1em of BOXCONTENT.north west] (top);
\coordinate [left=1em of BOXCONTENT.south west] (bottom);
\node(colm) [fit=(top)(bottom),inner sep=0pt,minimum width=1em,fill=Cfill] {};
%Because I could not shift the text to the left, not a beatufiul solution
\coordinate [left=0em of BOXCONTENT.north west] (top1);
\coordinate [left=1em of BOXCONTENT.south west] (bottom2);
\node(colm2) [fit=(top1)(bottom2),inner sep=-0pt,minimum width=1.1em,fill=Ccolm] {};
\end{tikzpicture}
}
\begin{document}
\begin{mybox}{15em}
This is the longer content
This is the longer content
This is the longer content
This is the longer content
This is the longer content
\end{mybox}
\end{document}
这个有效。代码来自 qtikz
\usetikzlibrary{positioning,fit}
\xdefinecolor{mycolor}{RGB}{62,96,111} % Neutral Blue
\definecolor{Ccolm}{gray}{0.6}
\definecolor{Cfill}{gray}{0.8}
\colorlet{bancolor}{mycolor}
\newenvironment{mybox}[2][]{%
\begin{tikzpicture}[#1]%
%
\node [inner sep=1pt,text width=#2,fill=Ccolm,]
(BOXCONTENT) \bgroup\rule{0pt}{3ex}{}
}{%
\egroup;
%Why they are slighlty bigger than their fit!!!
\coordinate [left=1em of BOXCONTENT.north west] (top);
\coordinate [left=1em of BOXCONTENT.south west] (bottom);
\node(colm) [fit=(top)(bottom),inner sep=0pt,minimum width=1em,fill=Cfill] {};
%Because I could not shift the text to the left, not a beatufiul solution
\coordinate [left=0em of BOXCONTENT.north west] (top1);
\coordinate [left=1em of BOXCONTENT.south west] (bottom2);
\node(colm2) [fit=(top1)(bottom2),inner sep=-0pt,minimum width=1.1em,fill=Ccolm] {};
\end{tikzpicture}
}
\begin{mybox}{15em}
This is the longer content
This is the longer content
This is the longer content
This is the longer content
This is the longer content
\end{mybox}
答案1
你应该为每项工作使用适当的工具;-) 这里是tcolorbox
\documentclass[12pt,border=5]{standalone}
\usepackage[most]{tcolorbox}
\newtcolorbox{mybox}[1][]{
enhanced,
colframe=gray!80,
colback=gray!40,
left=2em,right=1ex,top=1ex,bottom=1ex,%boxsep=1em,
leftrule=4pt,
rightrule=0pt,
toprule=0pt,
bottomrule=0pt,
arc=0pt,
%breakable, %% you may like these three lines
% nobeforeafter,
% enhanced jigsaw,
#1}
\begin{document}
\begin{mybox}[width=15em]
This is the longer content
This is the longer content
This is the longer content
This is the longer content
This is the longer content
\end{mybox}
\end{document}
关于你的代码,我认为你的做法可能会让你付出很大代价。最好不要这样做。但是,你可以通过添加来消除\node(colm)
和\node(colm2)
之间的大小差异\node(BOXCONTENT)
outer sep=0pt
\node [inner sep=1pt,outer sep=0pt,text width=#2,fill=Ccolm,]
(BOXCONTENT) \bgroup\rule{0pt}{3ex}{}