1

I'm using tcolorbox to make some 3x5 cards with lots of text on them. These cards usually consist of a few boxes that don't change much and one final box (containing descriptive text) that fills the rest of the card (using the [height fill] option). I usually use tcboxfit for the final box so that all the descriptive text fits nicely, but sometimes it just ends up too small.

I would like to somehow condition the options/type of the last box on its content---that is, if the fitted text is below a defined size threshold (in this example, 6pt), then nix the fitting and make a breakable box with a fixed text size (probably the 6pt lower threshold) where the second page will eventually be printed on the backside of my 3x5 card.

I've figured out how to condition something on the fitted text size using \tcbfitdim. I also discovered the nirvana option, which will execute everything given but not draw the box/contents. So far so good.

I thought to use a boolean flag to track whether or not the fitted text is too small and construct the appropriate box afterward, but the result doesn't want to propagate outside of the box itself. The following example always prints "false" regardless of what happened inside tcboxfit.

%%%%%%%%%% PREAMBLE %%%%%%%%%%
\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\usepackage{geometry}
\usepackage{printlen}
\usepackage{xifthen}

\newboolean{fittoosmall}

\geometry{paperwidth=3in,paperheight=5in,margin=0.05in,bottom=0.03in}

%%%%%%%%%% DOCUMENT %%%%%%%%%%
\begin{document}

    \tcbset{colback=white, colframe=black, beforeafter skip=0em}

    \newcommand{\boxcontent}{
        \lipsum[5]
        \lipsum[5]
        \lipsum[5]
        \lipsum[5]
        \par
    }

    \begin{tcolorbox}
        static box here
    \end{tcolorbox}
    \tcboxfit[height fill, fit basedim=12pt]{
        \boxcontent
        \ifthenelse{\lengthtest{\tcbfitdim<6pt}}{
            too small! \printlength{\tcbfitdim}
            \setboolean{fittoosmall}{true}
        }{
            all good! \printlength{\tcbfitdim}
            \setboolean{fittoosmall}{false}
        }
    }

    \ifthenelse{\boolean{fittoosmall}}{true}{false}

\end{document}

Beyond that, all I have is a vague sense that I might need to use pgfkeys somehow, but I'd rather not go there if I don't have to. Any tcolorbox aficionados care to weigh in? Is there a way to get my boolean strategy to work? Another strategy I should consider? I'm using LuaLaTeX for other parts of the project, so Lua-based ideas/solutions are welcome.

1 Answer 1

1

It's not incredibly elegant, but I found a solution. The TeX boolean didn't want to propagate outside of the tcolorbox environment, but assigning a Lua boolean variable instead did the trick. Do a test tcboxfit with the nirvana option to set the boolean and then make the real box afterward depending on the result. Example:

%%%%%%%%%% PREAMBLE %%%%%%%%%%
\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\usepackage{geometry}
\usepackage{printlen}
\usepackage{xifthen}
\usepackage{luacode}

\newboolean{fittoosmall}

\geometry{paperwidth=3in,paperheight=5in,margin=0.05in,bottom=0.03in}

%%%%%%%%%% DOCUMENT %%%%%%%%%%
\begin{document}

    \tcbset{colback=white, colframe=black, beforeafter skip=0em}

    \newcommand{\boxcontent}{
        \lipsum[5]
        \lipsum[5]
        \lipsum[5]
        \lipsum[5]
        \par
    }

    \begin{tcolorbox}
        static box here
    \end{tcolorbox}

    \tcboxfit[height fill, fit basedim=12pt, nirvana]{
        \boxcontent
        \ifthenelse{\lengthtest{\tcbfitdim<6pt}}{
            too small! \printlength{\tcbfitdim}
            \directlua{fittoosmallbool="true"}
        }{
            all good! \printlength{\tcbfitdim}
            \directlua{fittoosmallbool="false"}
        }
    }
    
    \ifthenelse{\equal{\directlua{tex.sprint(fittoosmallbool)}}{true}}{
        \begin{tcolorbox}[breakable,fontupper=\scriptsize]
            \boxcontent
        \end{tcolorbox}
    }{
        \tcboxfit[height fill, fit basedim=12pt]{\boxcontent}
    }

\end{document}

This only works if you're using LuaLaTeX and probably isn't the best solution for every use case, but it fits my needs just fine. Hopefully this is useful information for others in the future.

You must log in to answer this question.

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