4

I would like to separate blocks of text (stories) via a horizontal line inserted automatically between the blocks, but not before the first or after the last. I tried to achieve this by creating a flag with etoolbox to indicate whether the current block is the first, but this does not seem to work. The boolean always seems to evaluate to false. I have provided a simple example below, with the text "BETWEEN STORIES" rather than an hrule.

\documentclass{article}

\usepackage{etoolbox}

\newbool{didonestory}

\newenvironment{stories}{\boolfalse{didonestory}}{}
\newenvironment{story}{
    \ifbool{didonestory}{BETWEEN STORIES}{}
}{
    \booltrue{didonestory}
}

\begin{document}
\begin{stories}
    \begin{story}
        Test 1
    \end{story}
    \begin{story}
        Test 2
    \end{story}
\end{stories}
\end{document}

Here I expect the output to be "Test 1 BETWEEN STORIES Test 2" but instead the output is only "Test 1 Test 2".

My questions are:

  1. What am I doing wrong with the Boolean values?
  2. Is there a more standard way to achieve what I want here?

2 Answers 2

1

You want to set the boolean globally.

\documentclass{article}

\usepackage{etoolbox}

\newbool{didonestory}

\newenvironment{stories}{\global\boolfalse{didonestory}}{}
\newenvironment{story}{%
    \ifbool{didonestory}{BETWEEN STORIES}{}%
}{%
    \global\booltrue{didonestory}%
}

\begin{document}
\begin{stories}
    \begin{story}
        Test 1
    \end{story}
    \begin{story}
        Test 2
    \end{story}
\end{stories}
\end{document}

enter image description here

2
  • Would you recommend doing this rather than adding the hook, as suggested in the other answer? If so, why is this better?
    – Matt
    Commented Apr 13, 2023 at 10:25
  • 1
    @Matt I find this more efficient (and clearer).
    – egreg
    Commented Apr 13, 2023 at 10:35
6

you are setting the boolean to true in a group. Do if after the environment:

\documentclass{article}

\usepackage{etoolbox}

\newbool{didonestory}

\newenvironment{stories}{\boolfalse{didonestory}}{}
\AddToHook{env/story/after}{\booltrue{didonestory}}
\newenvironment{story}{\ifbool{didonestory}{BETWEEN STORIES}{}}{}

\begin{document}
\begin{stories}
    \begin{story}
        Test 1
    \end{story}
    \begin{story}
        Test 2
    \end{story}
\end{stories}
\end{document}

enter image description here

2
  • 1
    Thanks for your answer. Why does the booltrue go in a hook while the boolfalse does not? Surely they are both in groups?
    – Matt
    Commented Apr 12, 2023 at 20:32
  • 2
    the boolfalse is in the outer stories and that is ok. Commented Apr 12, 2023 at 20:39

You must log in to answer this question.

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