Sometimes when creating environments, you want to ensure that they always
- start new paragraph when two environments appear immediately after one another
- eat up spaces
- generally idiot-proof (which, dare I say, might also be called "fully LaTeX compatible")
I have seen a few techniques. Are there any recommended ways of doing this to avoid trouble?
\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\newenvironment{test}
{\par\noindent}% <-- before env hook
{}% <-- after env hook
\begin{document}
\begin{test}
\fbox{\parbox{\linewidth}{\lipsum[1]}}
\end{test}
\begin{test}
\fbox{\parbox{\linewidth}{\lipsum[1]}}
\end{test}
\begin{test}
\fbox{\parbox{\linewidth}{\lipsum[1]}}
\end{test}
\end{document}
Ideal Environment
This one can handle \begin{test}\end{test}\begin{test}\end{test} back to back. I used \par\noindent in the before environment hook. Is this the right way to do this? Based on this answer https://tex.stackexchange.com/a/22853/13552, it seems that I would also need \leavevmode. Could somebody clarify this and any other tips that are "good practice".




\leavevmodeas explained in the linked answer https://tex.stackexchange.com/questions/22852/function-and-usage-of-leavevmode/22853#22853? – Jonathan Komar Jun 20 '17 at 14:20\leavevmode; that answer deals with a completely different situation. – egreg Jun 20 '17 at 14:23