我拼命想画两个框,里面有一些右对齐的文本,最终应该像一个进度条。我的问题是,外矩形和内矩形之间有一些我无法消除的空间。如果我将最小高度设置得足够大,框至少会垂直对齐。这是我的最低限度高度(宽度)太小。理想情况下,我希望设置高度,使文本适合默认值inner sep
,宽度应为\dimexpr.7\linewidth\relax
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{xcolor}
\pagecolor{black!10} %% just to see the gap a bit better
\newlength{\mywidth}
\newlength{\myheight}
\begin{document}
\setlength{\mywidth}{\dimexpr.5\linewidth\relax}
\setlength{\myheight}{\baselineskip}
\begin{tikzpicture}
\node[fill=yellow!30,
minimum width=\dimexpr\mywidth+\pgfkeysvalueof{/pgf/inner xsep}\relax,
minimum height=\myheight] (inner){};
\node[draw=blue,
very thick,
minimum width=\mywidth,
text width=\mywidth,
minimum height=\myheight,
anchor=west,
align=right] (main) at (inner.west) {\hfill TEXTy};
\end{tikzpicture}
\setlength{\myheight}{5cm}
\begin{tikzpicture}
\node[fill=yellow!30,
minimum width=\mywidth,
minimum height=\myheight] (inner){};
\node[draw=blue,
very thick,
minimum width=\mywidth,
text width=\mywidth,
minimum height=\myheight,
anchor=west,
align=right] (main) at (inner.west) {\hfill TEXT};
\end{tikzpicture}
\par
\end{document}
答案1
我猜您正在寻找以下内容:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{myBOX/.style = {
box/.style = {draw=blue,very thick,
fill=yellow!30,
text width=\dimexpr0.5\linewidth
-2\pgfkeysvalueof{/pgf/inner xsep},
minimum height=##1, align=right}
}
}% end of tikzset
\begin{document}
\begin{tikzpicture}[myBOX]
\node[box=\baselineskip] {TEXTy};
\end{tikzpicture}
\begin{tikzpicture}[myBOX]
\node[box=5cm] {TEXT};
\end{tikzpicture}
\end{document}
附录: 我必须承认,我很难弄清楚你的问题是什么。正如你强调的那样,该图像应该类似于进度条,下面是模仿它的尝试:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit, positioning}
\newlength{\boxwidth}
\setlength{\boxwidth}{0.5\linewidth}
\tikzset{myBOX/.style = {
box/.style args = {##1/##2}{text width=##1\boxwidth,
minimum height=##2, inner ysep=2mm, inner xsep=0mm,
align=right, fill=yellow!30,
anchor=west,
append after command={\pgfextra{\let\LN\tikzlastnode}
coordinate (@aux) at (\boxwidth,0)
node [draw=blue, line width=1mm,
inner sep=0pt, fit=(\LN) (@aux)] {}
},
execute at end node=~~}
}
}% end of tikzset
\begin{document}
\begin{tikzpicture}[myBOX]
\node [box=0.7/\baselineskip] {TEXTy};
\end{tikzpicture}
\begin{tikzpicture}[myBOX]
\node [box=0.9/2\baselineskip] {TEXTy};
\end{tikzpicture}
\begin{tikzpicture}[myBOX]
\node [box=1/5cm] {TEXT};
\end{tikzpicture}
\end{document}
答案2
您需要设置inner sep=0pt
:
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{xcolor}
\pagecolor{black!10} %% just to see the gap a bit better
\newlength{\mywidth}
\newlength{\myheight}
\begin{document}
\setlength{\mywidth}{\dimexpr.5\linewidth\relax}
\setlength{\myheight}{\baselineskip}
\begin{tikzpicture}[inner sep=0pt]% <---- NOTE
\node[fill=yellow!30,
minimum width=\dimexpr\mywidth+\pgfkeysvalueof{/pgf/inner xsep}\relax,
minimum height=\myheight,
] (inner){};
\node[draw=blue,
very thick,
minimum width=\mywidth,
text width=\mywidth,
minimum height=\myheight,
anchor=west,
align=right,
] (main) at (inner.west) {\hfill TEXTy};
\end{tikzpicture}
\setlength{\myheight}{5cm}
\begin{tikzpicture}[inner sep=0pt]% <---- NOTE
\node[fill=yellow!30,
minimum width=\mywidth,
minimum height=\myheight,
] (inner){};
\node[draw=blue,
very thick,
minimum width=\mywidth,
text width=\mywidth,
minimum height=\myheight,
anchor=west,
align=right,
] (main) at (inner.west) {\hfill TEXT};
\end{tikzpicture}
\par
\end{document}