2

I have been trying to reformat the table and figure environments in a custom class file I am building. The idea is to put them in a box with a shaded background. I have been trying to accomplish this with the float package in combination with either mdframed or tcolorbox. Regardless of which of the latter I choose, I run into a problem where text in the float is typeset into the right margin. I assume, therefore that the problem is not with either mdframed or tcolorbox but with something that float does (though the behaviour is a little different; mdframed draws a box into the margin, but tcolorbox does not).

For various reasons, the solution should be backward compatible with the standard table and figure environments provided by the article.cls (on which my class is built). I am aware that tcolorbox allows some integration with custom figure environments, but I would like to format the standard environments.

I am not committed to using the float package; if anybody knows of anything better (and easier to use) I'd be glad to hear it.

Here is an MWE using tcolorbox:

\documentclass{article}

\usepackage{lipsum}

\usepackage{tcolorbox}
\usepackage{float}

\usepackage[]{geometry}

\makeatletter
% Create a new caption style.
\newcommand\floatc@sp[2]{\textsf{\textbf{#1} \hfill #2}}%

% Use tcolorbox to draw a box around the float.
\newcommand\fs@sp{
\def\@fs@cfont{\sf\bfseries}\let\@fs@capt\floatc@sp
\def\@fs@pre{\begin{tcolorbox}}%
\def\@fs@post{\end{tcolorbox}}%
\def\@fs@mid{\vspace{\abovecaptionskip}}
\let\@fs@iftopcapt\iffalse}

\makeatother

% Restyle the floats with the new style.
\floatstyle{sp}
\restylefloat{figure}
\restylefloat{table}

\begin{document}

\begin{tcolorbox}

This is a tcolorbox.

\lipsum[1]

\end{tcolorbox}

\begin{figure}
\caption{The text in this figure flows into right margin.}

\lipsum[1]

\end{figure}

\end{document}

Here is the output:

enter image description here

Many thanks for your consideration.

  • Welcome! Is it possible to create the custom float environments using tcolorbox and then just \let the standard float names to the custom ones? – cfr Jun 10 '18 at 23:55
2

Something like this?

figures within bounds

Without float, but with environ (not sure whether this was necessary or not):

\documentclass{article}
\usepackage{lipsum}
\usepackage{environ}
\usepackage{tcolorbox}
\usepackage{geometry}
\let\origfigure\figure
\let\endorigfigure\endfigure

\NewEnviron{myfigure}{%
  \begin{origfigure}
    \begin{tcolorbox}
      \BODY
    \end{tcolorbox}
  \end{origfigure}%
}

\let\figure\myfigure
\let\endfigure\endmyfigure

\begin{document}

\begin{tcolorbox}

This is a tcolorbox.

\lipsum[1]

\end{tcolorbox}

\begin{figure}
  \lipsum[1]
  \caption{The text in this figure stays within the  margins.}
\end{figure}

\begin{figure}
  \lipsum[2]
  \caption{Here's a caption which shouldn't flow anywhere, since the text in the figure doesn't do so.}
\end{figure}


\end{document}
2

tcolorbox offers blend into option to include some particular boxes inside already know environments like figures and tables. This way titles are considered captions and these titles appear in corresponding lists of figures or tables. These boxes can be also floating objects if you include float inside their definition.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{duckuments}

\newtcolorbox[blend into=figures]{myfigure}[2][]{
    float, title=#2, halign=center, #1}

\begin{document}
\listoffigures
\begin{myfigure}{This is a nice duck}
\includegraphics{example-image-duck}
\end{myfigure}

\begin{figure}[htb]
\centering
\includegraphics{example-image-duck}
\caption{Another nice duck}
\end{figure}

\end{document}

enter image description here

  • This was actually what I was referring to when I said that tcolorbox allowed some integration with custom figure environments, but I realize I could have been clearer about that. I issue with this solution is that it's not immediately backward compatible with the standard figure environment, though I imagine there is a way to do so. – Nick LaCara Jun 12 '18 at 20:08

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.