1

I like to create textboxes via TikZ. I start with that flexible example. And, Yes :) I do create texboxes. Here it is. enter image description here

But I still have two problems..

Q1

I use fit to create light gray rectangular on the left of the text box (reference). But \node(colm) and \node(colm2) are slightly bigger than \node(BOXCONTENT). Why? Any ideas? enter image description here

Q2

Also, I add a second rectangular in order to shift my text to the right inside the node because of my poor knowledge about how to escape from mybox environment. Is there a nicer way? (I am sure there is) I tried to use something like\setlength{\leftmargini}{2pt} but I am still confused about the part

...
(BOXCONTENT) {}\bgroup\rule{0pt}{3ex}
}{%
\egroup;
...

NOT WORKING!!! The following MWE is reproduces my results.

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

This one works. Code from 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}

trblnc
  • 451
  • The posted code generates ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. – David Carlisle Nov 10 '14 at 13:14
  • your example doesn't run so I can't test but are you not missing a % here (BOXCONTENT) {}\bgroup\rule{0pt}{3ex}%%%%% – David Carlisle Nov 10 '14 at 13:15
  • @DavidCarlisle There should be a ; before \bgroup, but then the code doesn't produce the picture given in the OP anyway. –  Nov 10 '14 at 13:34
  • Yeah, there is something wrong. It was running inside the qtikz, but it is giving an error when I compile with pdflatex as well. Truly sorry to waste your time. I am gonna find it what is the problem. – trblnc Nov 10 '14 at 13:37
  • @trblnc_dsg Can you post the code that compiled in qtikz? –  Nov 10 '14 at 13:42
  • @HarishKumar added. By the way I was also using inside LyX via Lulatex without error, without ; before \bgroup – trblnc Nov 10 '14 at 13:48

1 Answers1

1

You should use proper tool for each job ;-) Here it is 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}

enter image description here

Regarding your code, I am of the opinion that what you are doing may cost you lot. Better don't do things in that way. However, you can get rid of the size variations between \node(colm) and \node(colm2) and \node(BOXCONTENT) by adding outer sep=0pt in

\node [inner sep=1pt,outer sep=0pt,text width=#2,fill=Ccolm,]
            (BOXCONTENT) \bgroup\rule{0pt}{3ex}{}

enter image description here