2

With the following environment, I would like to make #3 an optional argument. In a way that when #3 is present, the text starts on the next line. And when #3 is missing, the text is written in the adjacent area as done by the theorem environment.

\NewDocumentEnvironment{Pz}{mmm}
  %% General Proposition
  %% \Pz {SPECIFIER} {LOCATOR} {TITLE}
  { \noindent #1 \ #2 \ (#3) }
  {}

\begin{document}

\maketitle
\tableofcontents

\chapter {This is an introduction
  to some topic}

\section{Some Section}

This section is about this and that.

\begin{Pz}{Theorem}{GCHQ}{Pythagoras}
  Some Text
\end{Pz}

\begin{Pz}{Theorem}{GCHQ}
  Some Text on new line
\end{Pz}

\end{document}

The following is not doing the trick

\NewDocumentEnvironment{Pz}{mmO{}}
  %% General Proposition
  %% \wvPz {SPECIFIER} {LOCATOR} {TITLE}
  %% TITLE Optional, Default Empty
  { \noindent #1 \ #2 \ (#3)
    \IfValueT {#3} { \newline }
  }
  {}
8
  • 1
    @JohnKormylo The bulk of the xparse documentation is now incorporated into usrguide.
    – Alan Munn
    Oct 10, 2023 at 3:07
  • Great, what's usrguide? I couldn't find it in CTAN and MikTeX. Oct 10, 2023 at 3:12
  • @JohnKormylo texdoc usrguide doesn't bring it up? (Maybe MikTeX doesn't install documentation by default?) It's part of the kernel documentation (the user version of clsguide, which is for package writers).
    – Alan Munn
    Oct 10, 2023 at 3:14
  • Okay, I found it in C:\Users\John\AppData\Local\Programs\MiKTeX\doc\latex\base Oct 10, 2023 at 3:19
  • Please don't edit the question to include code that was provided (in this case indirectly) by the answer. It makes the original question hard to understand, and it makes it appear that the question wasn't answered as asked, when in fact it was in this case.
    – Alan Munn
    Oct 10, 2023 at 4:11

1 Answer 1

5

Something like this. Optional arguments are represented by the o specifier, (or O{default} if you want an optional argument with a default value. You can then test whether the argument was provided by using the \IfNoValueTF{no arg}{arg} test, or one of its alternatives.

\documentclass{article}

\NewDocumentEnvironment{Pz}{mmo}
{\IfNoValueTF{#3}
    {\noindent #1 \ #2  }%
    {\noindent  #1 \ #2 \ (#3)\par}%
    }
    {}
\begin{document}

\begin{Pz}{Theorem}{GCHQ}[Pythagoras]
  Some Text
\end{Pz} 

\begin{Pz}{Theorem}{GCHQ}  Some Text
\end{Pz} 

\end{document}

output of code

18
  • Like that. Would not have guessed that I needed a repetition of the command for both true and false.
    – Veak
    Oct 10, 2023 at 3:14
  • There are other ways to write the code, but IMO this is cleaner and more readable even if it involves a little repetition. For example, you could also use {\noindent #1 \ #2 \IfNoValueF{#3}{\ (#3)\par}} as the begin environment code which would remove the repetition. If this is more readable to you, then you can do that.
    – Alan Munn
    Oct 10, 2023 at 3:15
  • For some reason \begin{Pz}{Pz}{ID}{TITLE} does not show (TITLE}. I only get TITLE.
    – Veak
    Oct 10, 2023 at 3:30
  • @Raksh Optional arguments are introduced by square brackets, [] not braces {}.
    – Alan Munn
    Oct 10, 2023 at 3:31
  • Right. It works good now that I have changed it.
    – Veak
    Oct 10, 2023 at 3:32

You must log in to answer this question.

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