0

I would like to have a secondary environment named \defin that uses a parent environment, the latter taking three arguments. \defin will call \statement with a hard-wired first argument, the rest of the arguments behaving as the parent.

Would this implementation be what I have to do, doing a simple test for #2 in \defin ?

Would like that \defin is exactly like \statement except that the first argument in set to Definition.

The problem is that \begin{defin}{This}[That] does not put That within () to get Definition This (That). But gives me Definition This That

\documentclass[a4paper,12pt]{article}

\begin{document}

\NewDocumentEnvironment{statement}{mmo} { \IfNoValueTF {#3} { \noindent #1 \ #2 , } { \noindent #1 \ #2 \ (#3) } } { \vspace{0.3cm} }

\NewDocumentEnvironment{defin}{mo} { \IfNoValueTF {#2} { \statement {Definition} {#1} } { \statement {Definition} {#1} {#2} }
{}

\begin{defin}{This} Some Text \end{defin}

\begin{defin}{This}[That] Some Text \end{defin}

\end{document}

Veak
  • 1
  • 2
    Welcome to TeX.SE! Can you please show us in a short compilable TeX code how you want to use your environments? Then we have not to guess what you are doing ... – Mensch Oct 10 '23 at 19:31
  • 1
    What's the reason for not using the theorem-like environments already provided by LaTeX? Anyway, you're missing the \endstatement instructions. – egreg Oct 10 '23 at 19:47
  • I want to learn to do my own things using simple examples like these. – Veak Oct 10 '23 at 19:50
  • Is statement supposed to be a command, to be called by \statement{one}{two}{optthree}, or an environment created by \NewDocumentEnvironment? Should the third argument to statement be optional as with {mmo}, or mandatory as with {Definition}{#1}{#2}? – Teepeemm Oct 10 '23 at 20:10
  • statement is an environment. defin should also be an environment. Basically defining an environment by calling a more general environment with the first argument prefilled. – Veak Oct 10 '23 at 20:13
  • Perhaps what I am trying to do is not possible. I now realise that I am calling statement as a command. What's one to do? – Veak Oct 10 '23 at 20:24
  • The third argument to statement is optional. defin is supposed to pass the second and third arguments of statement, the first one being Definition. – Veak Oct 10 '23 at 20:31
  • 4
    Given the examples it appears that you are changing your account name constantly which makes refering to earlier questions (even questions on the same day) problematic as it's not clear if you are the same person or not. – David Carlisle Oct 10 '23 at 20:33
  • Note that (with good reason) latex never uses \noindent to suppress indentation in this way, you should (as previously explained to someone who may be you) this should be a theorem environment or a one item list. – David Carlisle Oct 10 '23 at 20:34
  • why have you declared an environment statement which you then use as a command? If you want the command form why didn't you use \NewDocumentCommand ? As it is the code is unbalanced you call the start of statement but never the end. – David Carlisle Oct 10 '23 at 20:37
  • Are you referring to the use of {%. Indentation is not the biggest problem. Can one call the functionalities of an environment from another environment definition ? – Veak Oct 10 '23 at 20:37
  • 1
    No I am refering to \noindent even if you get the form used to not give errors it is not going to produce a good definition as it lacks all the features needed for a heading definition such as not allowing a page break after the heading. Yes you can use one environment in the definition of another but you are not doing that. – David Carlisle Oct 10 '23 at 20:39
  • 1
    impossible to say given the sketch you have shown, why do you want two definitions at all? As shown possibly you just want to declare \statement as a command, but perhaps you have some reason to have it as an environment. But also it's pointless having a conversation if all previous comments have to be ignored as the account has changed. – David Carlisle Oct 10 '23 at 20:41
  • Would like to see an example of using an environment in the definition of another. – Veak Oct 10 '23 at 20:41
  • statement is an environment I might use because of its generality. I can define any promise I want rather than being limited to Definition, etc as the first word.. – Veak Oct 10 '23 at 20:44

1 Answers1

3

If you use o for an optional argument, the argument always has to go in square (rather than curly) brackets.

Because statement is an environment, you also need to use

\begin{statement}

\end{statement}

You were also missing a closing brace, I think, in the definition of defin.

I'm not sure which horizontal spaces are intended and which may be spurious, so you may not want comment signs everywhere I've put them, but you probably want to be more careful with line endings and spaces if you want to avoid the introduction of mysterious spaces in the output.

\documentclass[a4paper,12pt]{article}

\begin{document}

\NewDocumentEnvironment{statement}{mmo} {% \IfNoValueTF {#3} {\noindent #1 \ #2 ,} {\noindent #1 \ #2 \ (#3)}% } {\vspace{0.3cm}}

\NewDocumentEnvironment{defin}{mo} {% \IfNoValueTF {#2} {% \begin{statement}{Definition}{#1}% }{% \begin{statement}{Definition}{#1}[#2]% }% }{% \end{statement}% }

\begin{defin}{This} Some Text \end{defin}

\begin{defin}{This}[That] Some Text \end{defin}

\end{document}

defin with and without an optional argument

cfr
  • 198,882
  • I never realised what the % was all about in {% and }%. Thought they were just comment lines with nothing. – Veak Oct 10 '23 at 20:47
  • 1
    @Barytoc You don't need them if you're using expl3 but, yes, otherwise unwanted spaces are a problem. And they can be a real pain to track down in code. The % is a comment, so you're turning what would otherwise be a space into a commented space. (Newline is a space.) – cfr Oct 10 '23 at 20:49
  • What would I need to do ? Call ExplSyntaxOn ? Does the manual describe them somewhere. The syntax is extremely weird. – Veak Oct 10 '23 at 20:57
  • 1
    And at times you have two as '%%'. Tolkien invented this ? – Veak Oct 10 '23 at 20:59
  • @Barytoc Sorry. Just noticed that. It's a typo I've now corrected. Tolkien would have come up with something more interesting. \ExplSyntaxOn means spaces and newlines are ignored. But you'd then have to explicitly add any spaces you do want. – cfr Oct 10 '23 at 21:01
  • What does one have to do to use expl3 so that one would not need the %. – Veak Oct 10 '23 at 21:10
  • @Barytoc If you do \ExplSyntaxOn, you don't need % for spaces until you say \ExplSyntaxOff and then you need to worry about spaces again. But it means you have to specify any spaces you do want explicitly e.g. \ExplSyntaxOn Some ~ text ~ here.\ExplSyntaxOff Now spaces count again.. Without the ~, you'd get Sometexthere.Now spaces count again. But you don't need to comment line endings etc. – cfr Oct 10 '23 at 23:15