3

I have a document full of examples that I created with a simple theorem. The problem now is that I try to modify the examples' appearance by adding rules at beginning and end. This turned to be harder than I thought, but I managed to do some progress. This is the MWE:

\documentclass[tikz]{article}
\usepackage{amsthm}
\usepackage{lipsum}

\newtheorem{FirstExample}{First example}


\newtheorem{SecondExampleImpl}{Second example}

\newtheorem{SecondExample}{}

\renewenvironment{SecondExample}[1]{
\par\nobreak\noindent\rule{\textwidth}{3pt}\par\addvspace{12pt}
\begin{SecondExampleImpl}{{#1}}}{\end{SecondExampleImpl}\par\nobreak\noindent\rule{\textwidth}{1pt}\par\addvspace{12pt}}

\begin{document}

\begin{FirstExample}[First example] \label{ex:first}
This is a first example...  
\end{FirstExample}

\lipsum[1]

Ref to first example~\ref{ex:first}.


\begin{SecondExample}[Second example] \label{ex:second}
This is a second example... 
\end{SecondExample}


\lipsum[1]

Ref to second example~\ref{ex:second}.

\end{document}

enter image description here

So there are 2 theorems defining the examples. The theorem FirstExample is the old one, and theorem SecondExample is the new one. At first sight it looks like the new theorem is doing its work, but actually you can notice that the theorem title is not written properly. It is as if the argument doesn't get passed to the other macro in the right way. Also, I'm not sure whether referencing the new macro will always be correct, although in this MWE it seems to work.

1 Answer 1

2

Do not pass the argument to the new environment.

b

\documentclass[tikz]{article}
\usepackage{amsthm}
\usepackage{lipsum}

\newtheorem{FirstExample}{First example}    

\newtheorem{SecondExampleImpl}{Second example}

\newtheorem{SecondExample}{}

\renewenvironment{SecondExample} % changed <<<<<<<<<<<<<
{\noindent\rule{\textwidth}{3pt}\par\addvspace{12pt}\begin{SecondExampleImpl}}
{\end{SecondExampleImpl}\noindent\rule{\textwidth}{1pt}\par\addvspace{12pt}}

\begin{document}
    
    \begin{FirstExample}[First example A] \label{ex:first}
        This is a first example...  
    \end{FirstExample}
    
    \lipsum[1]
    
    Ref to first example~\ref{ex:first}.
    
    
    \begin{SecondExample}[Second example B] \label{ex:second}
        This is a second example... 
    \end{SecondExample}
    
    
    \lipsum[1]
    
    Ref to second example~\ref{ex:second}.
    
\end{document}
5
  • That worked, thanks! Do you happen to know how to reduce the space between the top rule and the title? I tried removing the addvspace{12pt} to no avail and I don't think I have to use \vspace{-1em} or something similar, do I?
    – aaragon
    Feb 18, 2023 at 15:12
  • @aaragon Try \noindent\rule{\textwidth}{3pt}\\[-2em]\begin{SecondExampleImpl} Feb 18, 2023 at 15:54
  • Well the \vspace{1em} also does the trick, but now the problem is that I see the ruler and the title of the example not always stick together, sometimes they're split into different pages and that makes no sense. I didn't expect this to be this hard. Someone seems to have addressed this issue in this post but when I use the ntheorem package all sort of conflicts arise.
    – aaragon
    Feb 18, 2023 at 16:10
  • Won’t the mininpage solution turn the environment into a float? These examples sometime span over several pages, I don’t think that’s the right solution, or am I missing something?
    – aaragon
    Feb 19, 2023 at 12:53
  • As I had expected hell broke loose with the minipage. Now I get the error ./book.tex:98: LaTeX Error: Float(s) lost. since I can't have figures in examples...
    – aaragon
    Feb 19, 2023 at 14:26

You must log in to answer this question.

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