0

I wrote an "advanced" environment, but I am having problems if I put another environment inside:

\documentclass{article}

\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, symbols, etc
\usepackage{thmtools}

\newif\ifadvanced % advanced environment
\advancedtrue % comment out to hide advanced environments


\newcommand{\separator}{\noindent\makebox[\linewidth]{\rule{\linewidth}{0.4pt}}}
\newenvironment{advanced}{\ifadvanced\par\separator \small \fi}{\ifadvanced\\\separator\par\fi}

\declaretheoremstyle[%
  spaceabove=-6pt,%
  spacebelow=6pt,%
  headfont=\normalfont\itshape,%
  postheadspace=1em,%
  qed=\qedsymbol%
]{myproofstyle} 
\declaretheorem[name={Proof},style=myproofstyle,unnumbered,
]{advancedproof}

\begin{document}

\begin{advanced}
Inside advanced environment.
\begin{advancedproof}
inside proof
\end{advancedproof}
\end{advanced}
\end{document}

I could fix this problem by adding text between \end{advanced} and \end{advancedproof} but there must be a better way to solve this problem. I can't seem to find the answer, as I tried to add newlines inside the environments without luck.

UPDATE

The proposed code messes up the behavior where there is no inner environment:

\documentclass{article}

\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, symbols, etc
\usepackage{thmtools}

\newif\ifadvanced % advanced environment
\advancedtrue % comment out to hide advanced environments


\newif\ifadvanced % advanced environment
\advancedtrue % comment out to hide advanced environments


\newcommand{\separator}{\noindent\makebox[\linewidth]{\rule{\linewidth}{0.4pt}}}
\newenvironment{advanced}{\ifadvanced\par\separator \small \fi}{\ifadvanced\vspace*{-1em}\separator\par\fi}

\declaretheoremstyle[%
  spaceabove=-6pt,%
  spacebelow=6pt,%
  headfont=\normalfont\itshape,%
  postheadspace=1em,%
  qed=\qedsymbol%
]{myproofstyle} 
\declaretheorem[name={Proof},style=myproofstyle,unnumbered,
]{advancedproof}

\begin{document}

\begin{advanced}
Inside advanced environment.
\begin{advancedproof}
inside proof
\end{advancedproof}
\end{advanced}


\begin{advanced}
Inside advanced environment. But without any other internal environment
\end{advanced}

\end{document}
3
  • The problem is \ifadvanced\\ which you should replace, e.g. with \ifadvanced\hbox{}\\ or something else properly ending the environment.
    – TeXnician
    Feb 9, 2017 at 12:57
  • I don't have much experience defining environments. I had to use a flag to be able to remove advanced environments and thus I made it work using the code above. Do you have an example of what you mean by properly ending the environment?
    – aaragon
    Feb 9, 2017 at 13:07
  • I meant something which does not prevent the line break, e.g. a \vspace*{}.
    – TeXnician
    Feb 9, 2017 at 13:08

1 Answer 1

2

You could replace your code with the following. This just translates \\ to \vspace*{0pt}.

\documentclass{article}

\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, symbols, etc
\usepackage{thmtools}

\newif\ifadvanced % advanced environment
\advancedtrue % comment out to hide advanced environments


\newcommand{\separator}{\noindent\makebox[\linewidth]{\rule{\linewidth}{0.4pt}}}
\newenvironment{advanced}{\ifadvanced\par\separator \small \fi}{\ifadvanced\vspace*{0pt}\separator\par\fi}

\declaretheoremstyle[%
  spaceabove=-6pt,%
  spacebelow=6pt,%
  headfont=\normalfont\itshape,%
  postheadspace=1em,%
  qed=\qedsymbol%
]{myproofstyle} 
\declaretheorem[name={Proof},style=myproofstyle,unnumbered,
]{advancedproof}

\begin{document}

\begin{advanced}
Inside advanced environment.
\begin{advancedproof}
inside proof
\end{advancedproof}
\end{advanced}
\end{document}

Update: Your problem should be solved using

\newenvironment{advanced}{\ifadvanced\par\separator \small \fi}{\ifadvanced\vspace*{-.75em}\par\separator\par\fi}
4
  • In this way though, there is a big space between the last line of the advanced environment and the separator.
    – aaragon
    Feb 9, 2017 at 13:20
  • @aaragon You can of course adjust the value used (even negative). It's a vspace-command!
    – TeXnician
    Feb 9, 2017 at 13:21
  • Still it doesn't work. If I have no environment inside then it breaks! Look at the update.
    – aaragon
    Feb 9, 2017 at 13:45
  • @aaragon I've updated my answer (last lines) to meet your needs. If you have another \parskip please adjust the vspace.
    – TeXnician
    Feb 9, 2017 at 13:49

You must log in to answer this question.

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