3

I have a rectangular style in latex and I want to fix the size of that. however, in the setting I can only set the minimum width and minimum height. Here is my style;

direct/.style={rectangle, minimum height =4 cm, rounded corners=4.4mm, minimum width=10 cm, fill=red!15, draw,thick,},

And then the size of this shape change based on the text in it. I want a constant rectangular. Do you know how I can solve this? Thanks

1
  • You are trying to draw a rectangle as a style of a node. node have a content to respect {} and thus must keep a size that contains that content. If you want a rectangle of exact height and width, you need to draw it as a path aka: \draw (A) rectangle (B) where A and B are nodes or coordinates.
    – anis
    Commented Feb 10, 2023 at 8:33

3 Answers 3

4

Anis' comment already tells you a lot. Just as add-on, I want to mention few possibilities if you really have content in your nodes and would like to have a homogeneous size. Without knowing your context and needs, mine is just speculation. Hopefully it serves as starting point.

  • You could use text width set to the minimum width (or slightly less) and let TikZ wrap your text (to change alignment, use the align key). This approach prevents the node content, if too long, to exceed the minimum desired width, but it does not prevent the same for the height, though.
  • You could then define a style to scale by hand the font, using e.g. the relsize package. You would use this style to manually reduce the node content when needed. This requires some fine tuning, though.
  • You could automatise the previous approach, by getting inspired here. I took one of the answers there in my example below. The idea is to automatically scale the node content to fit a box that in turn fits your node desired size.

As general advice, you might have a look in general to the PGF manual, maybe starting with the minimal introduction to TikZ to get a general overview. Both documents are here.

Here a proof of concept.

\documentclass[border=1mm]{standalone}
\usepackage{tikz,lipsum,environ,relsize}
\usetikzlibrary{positioning}

\newcommand{\fixedWidth}{10cm}
\newcommand{\fixedHeigth}{4cm}

\tikzset{
    direct/.style={
        rectangle,
        minimum height=\fixedHeigth,
        minimum width=\fixedWidth,
        rounded corners=4.4mm,
        fill=red!15,
        draw,
        thick
    },
    fontsmaller/.style={font=\smaller[#1]}
}

% Taken from https://tex.stackexchange.com/a/26004/128737
\makeatletter
\NewEnviron{fitbox}[2]{%
    \minipage{#1}%
    \sbox0{\minipage{#1}\strut\BODY\strut\endminipage}%
    \Gscale@div\factor{#2}{\dimexpr\ht0+\dp0\relax}%
    \relscale{\factor}%
    \BODY
    \endminipage
}
\makeatother

\begin{document}
    \begin{tikzpicture}
        \node [direct] (N1) {Short};
        \node [direct, below = of N1] (N2) {
            Very long text whose width exceeds the specified minimum
            width of the node and hence the desired shape
        };
        \node [direct, below = of N2, text width=10cm] (N3) {\lipsum[1-2]};
        \node [direct, below = of N3, text width=10cm, fontsmaller=3.5] (N4) {\lipsum[1-2]};
        \node [direct, below = of N4, text width=10cm] {
            \begin{fitbox}{\fixedWidth}{\fixedHeigth}
                \lipsum[1-2]
            \end{fitbox}
        };
    \end{tikzpicture}
\end{document}

Result

2

Another option is to typeset the text in a fixed shape, and telling nothing about it to TikZ. If the text will not fit, it will leak...

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{tikz}
\newcommand{\FixedSizeText}[3]{% width, height
    \begin{minipage}[c][#2][c]{#1}%
        #3%
    \end{minipage}%
}
\begin{document}
\begin{tikzpicture}[]
    \tikzset{my rect/.style={
        rectangle, draw=red, inner sep=0pt, rounded corners},
    }
    \path (0,0) node [my rect]{\FixedSizeText{3cm}{2cm}{A little text}}
        (3,0) node [my rect]{\FixedSizeText{3cm}{2cm}{A little text}}
        (0,-3) node [my rect]{\FixedSizeText{3cm}{2cm}{Longer text that will arguably not fill in the minipage, and so the thing will be leaked around}}
        ;
\end{tikzpicture}
\end{document}

enter image description here

1

Perhaps You search for something like this (equals boxes, not the same quantity of text inside): enter image description here

Code:

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}

\begin{document}

    \begin{tcolorbox}[enhanced,fit to height=8cm, width=10cm,
        colback=gray!35!black!20!white,colframe=gray!75!black,title=Fit box (8cm),
        drop fuzzy shadow]
        \lipsum[1-2]
    \end{tcolorbox}
    \quad
    \begin{tcolorbox}[enhanced,fit to height=8cm, width=10cm,
        colback=gray!35!black!20!white,colframe=gray!75!black,title=Fit box (8cm),
        drop fuzzy shadow]
        \lipsum[1-3]
    \end{tcolorbox}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .