0

This is re: egreg's solution to this question.

Issue:

I would like the \NameOne command to replicate the \NameTwo command where it outputs only when being called on like so: \NameOne. When an argument is being entered into the command: \NameOne{} or \Name{Argument} it prints nothing and is only defining a global definition (as done in \NameTwo).

MWE:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\NameOne}{g}
 {
  \IfNoValueF{#1}
   {
    \tl_gset:Nn \g_jalep_nameone_tl {#1}
   }
  \tl_use:N \g_jalep_nameone_tl 
 }
\tl_new:N \g_jalep_nameone_tl
\ExplSyntaxOff

\newcommand{\NameTwo}[1]{\xdef\NameTwo{#1}}

\begin{document}
\textbf{NameOne}
\par This should say nothing: \NameOne{Argument}
\par This should say Argument: \NameOne
\par \hrulefill
\par \textbf{NameTwo}
\par This should say nothing: \NameTwo{Argument}
\par This should say Argument: \NameTwo
\end{document}

Output:

enter image description here

Edit:

I realize my question has been previously answered here. If a moderator thinks this question is a repeat, feel free to mark it as such.

  • you should think really hard before using g option in any distributed code, it breaks all latex syntax guidelines. It is useful in some very special circumstances but should not be used for general latex document commands. – David Carlisle Jan 15 '19 at 20:03
  • Did you try \NameTwo{élite}? – egreg Jan 15 '19 at 21:28
  • @DavidCarlisle I will be careful about using this g option. I'm doing all of this for my own curiosity and to better understand the TeX -> LyX relationship, but all in experimentation. @egreg I'm not quite sure I understand? – jed Jan 15 '19 at 21:35
3

Just use both possible branches of \IfNoValue (namely \IfNoValueTF):

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\NameOne}{g}
  {
    \IfNoValueTF{#1}
      {
        \tl_use:N \g_jalep_nameone_tl 
      }
      {
        \tl_gset:Nn \g_jalep_nameone_tl {#1}
      }
  }
\tl_new:N \g_jalep_nameone_tl
\ExplSyntaxOff

\newcommand{\NameTwo}[1]{\xdef\NameTwo{#1}}

\begin{document}
\textbf{NameOne}
\par This should say nothing: \NameOne{Argument}
\par This should say Argument: \NameOne
\par \hrulefill
\par \textbf{NameTwo}
\par This should say nothing: \NameTwo{Argument}
\par This should say Argument: \NameTwo
\end{document}

enter image description here

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.