I would like to create a structural delimiter that behaves identically to \section but compiles conditionally. In the MWE below I have labeled it \draftsection. The content of this section should only compile if \draftmodetrue. How should I define \draftsection?

% arara: lualatex




        \lipsum{2-3} % Always show this

        %  if \draftmodefalse do not compile anything from here
        \lipsum{3-4} %(should not be shown, since \draftmodefalse)
        %  To here

         %% if \draftmodetrue, everything should compile identically to the case \draftsection were replaced by \section

        \lipsum{4-5}  % Since this is not a draft section it should always be shown


I also know that I can create a conditional comment as in the code below:


I'm hoping that I can find a solution that (effectively) automatically places the end brace } of the \draftcomment above at the start of the next section.

That is I think I basically want

\draftcomment{\section {section content} }

Where {section content} is everything between Cherry and Durian. The problem is that I don't know how to get latex figure out what {section content} is automatically.

How does LaTeX keep track of which content is in which section? Clearly for equation numbering latex keeps track of which chapter the equation arises in.

I usually compile with lualatex, so I'm OK with solutions that require that, or not.

  • 1
    You can use the comment package. It provide a way to define environments that can be conditionally included or not. – Alain Merigot Nov 1 '19 at 1:01
  • This sounds promising, can you provide or link to an example. Part of what I'm hoping is that the \end{environment} logic will be taken care of by the start of the next section. – Canaryyellow Nov 1 '19 at 1:03
  • Here is the comment package documentation. But adding it in the section command will be complex. This is an environment that, as all envs, must be opened and closed... What about the first \section? And what will close the last one? IMHO using an environment (that can have a section name as parameter) is more flexible. Maybe you would like to exclude subsections, or a bunch of paragraphs, etc. – Alain Merigot Nov 1 '19 at 1:14

You could define \draftsection in the following way:

  \section{#1}% Print regular \section{<title>} for \draftsection{<title>}
    #2% Only print content between \draftsection and next \section if in draft mode
  \section% Restore captured \section for next \section

Here's what it does:

  • It prints \section{<title>} whenever you use \draftsection{<title>} (regardless of the state of \ifdraftmode;

  • Content between \draftsection{<title>} and a subsequent \section is only printed if \draftmodetrue.

  • It requires there to be an explicit \section following the \draftsection{<title>}. As such, you cannot have a \draftsection{<title>} as the last section within your document.

  • The definition needs to be updated if you plan on using a * (starred, unnumbered) version, or need an optional argument (as in \draftsection[<ToC entry>]{<title>}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.