4

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

2 Answers 2

4

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

1
  • Thanks for sharing, I like that the inner sep is still respected, but I would need a solution, where the yellow rectangle is not filling up the full box with the text still right aligned (like a progress bar).
    – thothal
    Mar 17 at 23:14
3

You need to set inner sep=0pt:

enter image description here

Code:

\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}
3
  • Yeah, I played also with inner sep but in fact I'd like to still have a inner margin. Is there a solution where I have the inner margin and no gap between yellow and blue rectangle?
    – thothal
    Mar 17 at 23:16
  • I am not sure I undertand what the difference is with your comment and the figure you have in the question. You can include the inner sep=0pt specifcation in the node if you don't want it for all nodes. If you need fine control perhaps separating the drawing, fillling and text placement is what you need. Mar 18 at 6:11
  • 1
    I guess that splitting the boxes into 3 parts is indeed the way to go. having the inner one with inner sep = 0pt and another innter one without filling and the text but with default inner sep will do what I need. Thanks!
    – thothal
    Mar 21 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.