- I would like to define a command
\dialoguestart{black!10}{black!25}
that starts setting the paragraphs intotcolorboxes
, alternating between lighter (black!10) and darker (black!25) ones.- The default setting of paragraphs should be returned to after another command
\dialogueend
Edit: After discussion with @Schrödinger's cat, @siracusa and @cfr it became clear that the initial question was too big and too unspecific. It emerged that a reasonable way to tackle the issue is using an environment and I have adjusted the post accordingly.
To illustrate, the snippet
\documentclass{article}
\usepackage{lipsum}
\usepackage[breakable]{tcolorbox}
%\input{solution_to_this_problem}
\begin{document}
\lipsum[66]
\dialoguestart{black!10}{black!25}
\lipsum[75]
\lipsum[66]
\dialogueend
\lipsum[75]
\end{document}
should produce
The MWE below produces the desired result. It does not take me all the way, however, as the new environment can only handle one paragraph at a time.
\documentclass{article}
\usepackage{lipsum}
\usepackage[breakable]{tcolorbox}
\newif\iflight\lightfalse
\newcommand\toggleboxstart{%
\iflight\begin{dark}%
\else\begin{light}\fi}
\newcommand\toggleboxend{%
\iflight\end{dark}\global\lightfalse%
\else\end{light}\global\lighttrue\fi}
\newenvironment{toggling}[2]{%
\newtcolorbox{light}{colback=#1,colframe=#1,sharp corners,breakable}
\newtcolorbox{dark}{colback=#2,colframe=#2,sharp corners,breakable}
% something that ensures `\toggleboxend\toggleboxstart` being called between paragraphs
}
\newcommand\dialoguestart[2]{\begin{toggling}{#1}{#2}\toggleboxstart}
\newcommand\dialogueend{\toggleboxend\end{toggling}}
\begin{document}
\lipsum[66]
\dialoguestart{black!10}{black!25}
\lipsum[75]
\dialogueend % these two commands
\dialoguestart{black!10}{black!25} % I would like to avoid
\lipsum[66]
\dialogueend
\lipsum[75]
\end{document}
To handle multiple paragraphs, \toggleboxend
and \toggleboxstart
have to be called between paragraphs within the toggling environment. I have tried adding
\let\origpar\par
\let\origeverypar\everypar
\renewcommand\everypar{\origeverypar\toggleboxstart}
\renewcommand\par{\toggleboxend\origpar}
to the environment's definition, which throws Paragraph ended before \renew@command was complete
. And pushing my commands using the {everyhook} package, which solved this problem similar to the one at hand
\usepackage{everyhook}
\newenvironment{toggling}[2]{%
\newtcolorbox{light}{colback=#1,colframe=#1,sharp corners,breakable}
\newtcolorbox{dark}{colback=#2,colframe=#2,sharp corners,breakable}
\PushPreHook{par}{\toggleboxstart}
\PushPostHook{par}{\toggleboxend}
}
\newcommand\dialoguestart[2]{\begin{toggling}{#1}{#2}} % no more \toggleboxstart
\newcommand\dialogueend{\end{toggling}} % no more \toggleboxend
results in being prompted TeX capacity exceeded, sorry [grouping levels=255]
.
What else can I do to execute some code between paragraphs? Within the environment, there will only be plain text.
Edit (by jakun who started the second bounty): My attempt to copy and paste my code into the bounty description did not work out well so I post it here again for better readability:
\documentclass{article}
\usepackage{lipsum}
\usepackage[breakable]{tcolorbox}
\newif\iflight
\newenvironment{toggling}[2]{%
\newcommand\toggleboxstart{%
\iflight
%(BEGIN LIGHT)
\begin{tcolorbox}[colback=#1,colframe=#1,sharp corners,breakable]%
\else
%(BEGIN DARK)
\begin{tcolorbox}[colback=#2,colframe=#2,sharp corners,breakable]%
\fi
}%
\newcommand\toggleboxend{%
\end{tcolorbox}\relax
%(END)
\iflight
\global\lightfalse
\else
\global\lighttrue
\fi
}%
\toggleboxstart
\everypar{\toggleboxend\toggleboxstart}%
}{%
\toggleboxend
}
\begin{document}
\lipsum[66]
\begin{toggling}{black!10}{black!25}
\lipsum[75]
\lipsum[66]
\lipsum[66]
\end{toggling}
\lipsum[75]
\end{document}
This code fails with
LaTeX Error: \begin{tcolorbox} on input line 36 ended by \end{tcb@savebox}.
or
Extra }, or forgotten \endgroup.
when commenting out the optional arguments of \begin{tcolorbox}
.
Why does this not work and how could one make it work?
(When commenting out the \begin{tcolorbox}
and \end{tcolorbox}
and commenting in the test text in parentheses I get the expected output.)
Edit: I am using the following versions (grep -i version main.log
):
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Arch Linux) (preloaded format=pdflatex 2019.11.15) 25 NOV 2019 21:59
Package: tcolorbox 2019/09/19 version 4.21 text color boxes
(/usr/share/texmf-dist/tex/latex/pgf/compatibility/pgfcomp-version-0-65.sty
Package: pgfcomp-version-0-65 2019/08/03 v3.1.4b (3.1.4b)
(/usr/share/texmf-dist/tex/latex/pgf/compatibility/pgfcomp-version-1-18.sty
Package: pgfcomp-version-1-18 2019/08/03 v3.1.4b (3.1.4b)
Library (tcolorbox): 'tcbbreakable.code.tex' version '4.21'
[Loading MPS to PDF converter (version 2006.09.02).]
I have tried using tcolorbox 2019/11/15 version 4.22 instead and get the same result.
\everypar
would be a way to go, see e.g. this answer: tex.stackexchange.com/a/119760.\everypar
to begin and\par
to end the respective environments. But both commands are called too often by LaTeX itself. My code now causes too many problems for me to fix. This might work, but goes beyond my knowledge and does not yet produce an MWE...tcolorbox
with thebreakable
option to make the above boxes work.)\begin{highlightingpars} ... [lots of paragraphs] \end{highlightingpars}
that would be easier and potentially less fragile. (It depends what you want to highlight. Does that material itself includetcolorbox
es, for example?) Right now, I think this question is either too big (i.e. can't be reasonably done here) or ill-defined (i.e. there are constraints we don't know).