7

This is a follow up question to this comment. How can I make campa's code for TeXbook's bend symbol work with the theorem environment?

Consider the following LaTeX code which I saved in a file called test.tex:

\documentclass{scrartcl}
\usepackage{tikz}
\newenvironment{mydanger}[1]{%
  \sbox0{%
     \begin{tikzpicture}[scale=.3]
        \draw[rounded corners=.1] (-.05,-1.5)--++(0,2.55)--++(.1,0)--++(0,-2.55);
        \draw[fill=white,rounded corners=1] (0,1)--(1,0)--(0,-1)--(-1,0)--cycle;
        \draw[very thick](-.3,-1.5)--(.3,-1.5);
        \node at (0,0) {\small#1};
     \end{tikzpicture}
  }%       this space ^^^ is here for a reason!
  \par\medskip  \noindent
  \hangindent\wd0
  \parindent\hangindent
  \hangafter=-2
  \setbox0=\hbox to0pt{\hss\lower3ex\box0}%
  \dp0=0pt
  \box0
  \small
  \ignorespaces
 }{\par\smallskip}
\newtheorem{theorem}{Theorem}
\usepackage{lipsum}
\begin{document}
\begin{mydanger}{A}
\lipsum[1]
\end{mydanger}

\vspace{1cm}

\begin{mydanger}{A} \begin{theorem} \lipsum[1] \end{theorem} \end{mydanger} \end{document}

When I run this code with lualatex test, the following output is typeset:

Bourbaki's bend symbol with the theorem environment

I'd like the theorem paragraph to flow around the bend symbol like the regular paragraph above it.


Important requirement

The solution must be consistent with the possibility of adding margin notes via \marginpar inside the theorem. Therefore, for instance, a solution that encapsulates the theorem inside an invisible tcolorbox will not be an acceptable solution; see here.

Evan Aad
  • 11,066

1 Answers1

2

This is a rather hacking solution. The implementation of theorem uses trivlist, which starts a new line. Basically what I did is to use \patchcmd to remove trivlist and redefine the \item command to make sure the output is consistent.

enter image description here

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{etoolbox}

\newenvironment{mydanger}[1]{% \sbox0{% \begin{tikzpicture}[scale=.3] \draw[rounded corners=.1] (-.05,-1.5)--++(0,2.55)--++(.1,0)--++(0,-2.55); \draw[fill=white,rounded corners=1] (0,1)--(1,0)--(0,-1)--(-1,0)--cycle; \drawvery thick--(.3,-1.5); \node at (0,0) {\small#1}; \end{tikzpicture} }% this space ^^^ is here for a reason! \par\medskip \noindent \hangindent\wd0 \parindent\hangindent \hangafter=-2 \setbox0=\hbox to0pt{\hss\lower3ex\box0}% \dp0=0pt \box0 \small \ignorespaces }{\par\smallskip} \newtheorem{theorem}{Theorem} \usepackage{lipsum}

\makeatletter \patchcmd{@begintheorem}{\trivlist\item}{\bgroup\myitem}{}{\GenericError{}{unable to patch command}{}{}}

\patchcmd{@opargbegintheorem}{\trivlist\item}{\bgroup\myitem}{}{\GenericError{}{unable to patch command}{}{}}

\def@endtheorem{\egroup} \def@opargendtheorem{\egroup} \def\myitem[#1]{#1~} \makeatother

\begin{document}

\begin{mydanger}{A} \lipsum[1] \end{mydanger}

\vspace{1cm}

\begin{mydanger}{A} \begin{theorem} \lipsum[1] \end{theorem} \end{mydanger}

\begin{mydanger}{B} \begin{theorem}[Test Theorem] \lipsum[1] \end{theorem} \end{mydanger}

\end{document}

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{adjustbox}

\newenvironment{mydanger}[1]{%
  \sbox0{%
     \begin{tikzpicture}[scale=.3]
        \draw[rounded corners=.1] (-.05,-1.5)--++(0,2.55)--++(.1,0)--++(0,-2.55);
        \draw[fill=white,rounded corners=1] (0,1)--(1,0)--(0,-1)--(-1,0)--cycle;
        \draw[very thick](-.3,-1.5)--(.3,-1.5);
        \node at (0,0) {\small#1};
     \end{tikzpicture}
     \hspace*{3mm}
  }%       this space ^^^ is here for a reason!
  \par\medskip  \noindent
  \hangindent\wd0
  \parindent\hangindent
  \hangafter=-2
  \setbox0=\hbox to0pt{\hss\lower3ex\box0}%
  \dp0=0pt
  \box0
  \small
  \ignorespaces
 }{\par\smallskip}
\newtheorem{theorem}{Theorem}
\usepackage{lipsum}

\makeatletter
\patchcmd{\@begintheorem}{\trivlist\item}{\bgroup\myitem}{}{\GenericError{}{unable to patch command}{}{}}
\patchcmd{\@opargbegintheorem}{\trivlist\item}{\bgroup\myitem}{}{\GenericError{}{unable to patch command}{}{}}

% also remove the spacing before Theorem
\patchcmd{\@begintheorem}{\hskip\labelsep}{}{}{\GenericError{}{unable to patch command}{}{}}
\patchcmd{\@opargbegintheorem}{\hskip\labelsep}{}{}{\GenericError{}{unable to patch command}{}{}}

\def\@endtheorem{\egroup}
\def\@opargendtheorem{\egroup}
\def\myitem[#1]{\noindent {#1}~}
\makeatother



\begin{document}

\makeatletter
\meaning\@begintheorem
\makeatother

\begin{mydanger}{A}
\lipsum[1]
\end{mydanger}

\vspace{1cm}

\begin{mydanger}{A}
\begin{theorem}
\lipsum[1]
\end{theorem}
\end{mydanger}

\begin{mydanger}{B}
\begin{theorem}[Test Theorem]
\lipsum[1]
\end{theorem}
\end{mydanger}

\end{document}
Alan Xiang
  • 5,227
  • Thank you. I can't test it right now, since I'm not near a computer with a TeX installation. One thing, though. In your solution tthe text flows jaggedly round the sign. Compare this to the way the paragraph flows evenly round the sign in the TeXbook, here, Could you emulate this even flow? – Evan Aad Jul 16 '21 at 16:17
  • 1
    I also included a second version below, which emulates the TeX book layout. – Alan Xiang Jul 16 '21 at 17:08
  • Thank you! I'll test it tomorrow. – Evan Aad Jul 16 '21 at 18:07
  • Why does the second piece of code print a strange first line? – Evan Aad Jul 18 '21 at 09:10
  • 1
    I used \meaning to analyze the command. Just forgot about it, you can simply delete it in the document body. – Alan Xiang Jul 18 '21 at 13:56
  • I'll accept your answer, because you met the requirements I specified in my original post. However, your code fails to work in the actual document I'm working on. In fact, just copying your preamble to my preamble without even using the \mydanger command in the body of the document, causes the compilation to fail with the error message unable to patch command. Unfortunately, I don't have the time currently to debug it. – Evan Aad Jul 19 '21 at 08:54
  • If \patchcmd doesn't work, you can simply extract the definition of \@begintheorem and manually redefine it with \renewcommand – Alan Xiang Jul 19 '21 at 20:42
  • I don't know what you mean by that. Could you change your code accordingly, please? – Evan Aad Jul 20 '21 at 01:00
  • @EvanAad Can you give a more relevant example of which document class/packages that you are using? It is likely that \patchcmd doesn't not work because of some changes to the definition of \theorem. – Alan Xiang Jul 21 '21 at 22:21