Draw a bordered box around a filled box with TikZ

alignmentmiktextikz-pgf

I am desperately trying to draw two boxes, with some right aligned text which should eventually resemble a progress bar. My problem is that there is some space between the outer and the inner rectangle which I cannot get rid of. If I set the minimum height large enough the box align at least vertically. That is my minimum height (width) is too small. Ideally I want to set the height such that the text fits with the default inner sep and the width should be \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}

Unwanted Gap between TikZ Rectangles

Best Answer

I guessing that you looking for the following:

enter image description here

\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}

Addendum: I must confess that I have problems to figured out what is your problem. As You emphasize, that image should be something like progress bar, here is a try to mimic it:

\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}

enter image description here