TransWikia.com

How to define section to compile conditionally

TeX - LaTeX Asked by Canaryyellow on October 14, 2020

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
documentclass[10pt,twocolumn]{book}
usepackage[margin=0.5in,showframe]{geometry}

usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage{graphicx}

usepackage{lipsum}


newififdraftmode
draftmodefalse

begin{document}
    chapter{Apple}
        lipsum{1-2}
    section{Banana}
        lipsum{2-3} % Always show this

    draftsection{Cherry}
        %  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

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

end{document}

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

newififdraftmode
DeclareRobustCommand{draftcomment}[1]{
ifdraftmode
#1
fi
}

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.

One Answer

You could define draftsection in the following way:

longdefdraftsection#1#2section{%
  section{#1}% Print regular section{<title>} for draftsection{<title>}
  ifdraftmode
    #2% Only print content between draftsection and next section if in draft mode
  fi
  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>}).

Answered by Werner on October 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP