1

With the following code I get almost what I would like to have (exclamation mark besides a text including an equation), but I get an overfull \hbox message although I am respecting textwidth.

The second problem is that the equation numbers are not aligned.

Perhaps minipages are not the appropriate tool?

\documentclass{article}
\usepackage{blindtext}

\begin{document}

\newenvironment{myenv}{\par\medskip \begin{minipage}[c]{0.05\textwidth}{\huge !}\end{minipage}\begin{minipage}[c]{0.95\textwidth}}{\end{minipage}\par\medskip}
This is a text.

\begin{myenv}
\blindtext

\begin{equation}
a=b\;.
\end{equation}
\end{myenv}

This a another equation:
\begin{equation}
c=d\;.
\end{equation}
\end{document}

Example

1
  • 2
    put \noindent before the first minipage Jan 21, 2020 at 10:35

1 Answer 1

2

As David Carlisle pointed out, your overfull \hbox is caused by the fact that the first minipage created by your environment starts a new paragraph. TeX automatically inserts a paragraph indentation box of width \parindent at the start of a paragraph unless you call \noindent. Here, you can put \noindent before the first \begin{minipage} in your definition. Alternatively, you can set \parindent to 0pt in your document if you want to get rid of paragraph identation; but do that globally rather than in the environment—that would be confusing. You may also consider using the parskip package for this purpose.

If you apply one of these suggestions and remove paragraph indentation (either locally with \noindent or globally using \parindent), there will still remain the problem you mentioned regarding alignment of the equations. That is quite expected, because display formulas are centered within the line width by default. Inside your environment, lines start more to the right than in the main text, but in both cases, lines end at the exact same right margin once you have removed paragraph indentation (this is because 0.05 + 0.95 = 1). In these conditions, the invisible vertical line around which display formulas are centered is necessarily more to the right (precisely by 0.025\textwidth) inside your environment than in the main text.

I propose to use the great tcolorbox package to do the formatting you desire. I'll present two ways to define a suitable warning environment, with possible options applicable to each way. The first way reduces the line width within the environment, and therefore has the same “problem” with alignment of display formulas that we've explained. The second way uses the same line width inside the environment and in the main text, and thus doesn't have this problem (or feature :-). In order to do this, it puts the decorative line and exclamation mark in the left margin.

First way: reduced line width

\documentclass{article}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\usepackage{lipsum}                % only for demo code

\definecolor{warningBoxColor}{rgb}{0.4,0.5,0.8}

\newtcolorbox{warning}[1][]{
   blanker, left=1.5cm, right=0mm,
   borderline west={0.7mm}{1.1cm}{warningBoxColor}, breakable,
   overlay unbroken and first={
     \begin{tcbclipframe}
        \coordinate (X) at ([xshift=5mm, yshift=-3mm]frame.north west);
        \node[circle, inner sep=1mm, color=white, fill=warningBoxColor,
              font=\bfseries] at (X) {!};
     \end{tcbclipframe}
   }, #1
}

\begin{document}

\lipsum[1][1-4]

\begin{warning}
\lipsum[1][5-12]
\begin{equation}
a = b
\end{equation}
\end{warning}

This is a another equation:
\begin{equation}
c = d
\end{equation}

\begin{warning}
\lipsum[1-6]
\begin{equation}
e = f
\end{equation}
\end{warning}

\end{document}

screenshot

Second way: same line width as in the main text

\documentclass{article}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\usepackage{lipsum}                % only for demo code

\definecolor{warningBoxColor}{rgb}{0.4,0.5,0.8}

\newtcolorbox{warning}[1][]{
   blanker, borderline west={0.7mm}{-5mm}{warningBoxColor}, breakable,
   before skip=2ex plus 0.1ex, after skip=2ex plus 0.1ex,
   overlay unbroken and first={
     \coordinate (X) at ([xshift=-10mm, yshift=-3mm]frame.north west);
     \node[circle, inner sep=1mm, color=white, fill=warningBoxColor,
           font=\bfseries] at (X) {!};
   }, #1
}

\begin{document}

\lipsum[1][1-4]

\begin{warning}
\lipsum[1][5-12]
\begin{equation}
a = b
\end{equation}
\end{warning}

This is a another equation:
\begin{equation}
c = d
\end{equation}

\begin{warning}
\lipsum[1-6]
\begin{equation}
e = f
\end{equation}
\end{warning}

\end{document}

screenshot

As you can see, this way the equations are all aligned around the same invisible vertical line.

Customization

Vertical centering of the exclamation mark

A first thing you might want to customize is the placement of the exclamation mark. Using yshift=-0.5*\tcbtextheight instead of yshift=-3mm in the \coordinate (X) at (...), you can vertically center it with respect to the boxed contents:

screenshot

This screenshot was obtained by applying the change to the second example.

Vertical space before and after the box

If you closely compare the “first way” and the “second way”, you'll also note that the latter has a bit more vertical space before and after the warning box than the former. This is because I used before skip=2ex plus 0.1ex and after skip=2ex plus 0.1ex in the second example, but not in the first one.

Passing arbitrary tcolorbox options

Finally, the warning environment I defined (in both examples) accepts an optional argument that you can use to pass any tcolorbox option(s) that you might need “sometimes, but not always”—otherwise, you would of course put them in the environment definition.

For instance, you can use the /tcb/show bounding box key in the first example in order to visualize the precise space occupied by the box created by tcolorbox:

\lipsum[1][1-4]

\begin{warning}[show bounding box]
\lipsum[1][5-12]
\begin{equation}
a = b
\end{equation}
\end{warning}

screenshot

This particular option (/tcb/show bounding box) is useful for debugging; you can find many other options in the tcolorbox manual.

You must log in to answer this question.

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