1

Is it possible to expand the \gls command in order to print it's "output" in a text file?

Here a 'MWE.tex' file content:

\documentclass{article}
\usepackage{glossaries-extra}
\makenoidxglossaries

\newglossaryentry{myglsentry}{name={Myglsentry},description={myglsentry desc}}

\newwrite\mytextfile \immediate\openout\mytextfile=\jobname.txt

\begin{document} \glsaddall

\immediate\write\mytextfile{\glsname{myglsentry}}

\immediate\closeout\mytextfile

\printnoidxglossary \end{document}

This MWE gives this result in 'MWE.txt': enter image description here

How can I obtain this result instead (i.e. the "output" of the \glsname{myglsentry} command call): enter image description here

zetyty
  • 779

1 Answers1

1

One way is to expand the argument of \write once with \unexpanded\expandafter (see for example Partially expanding a command and `\unexpand\expandafter` add spaces : how to append a text at the end of a file) and use the most low-level macro from glossaries, that contains the contents with a single expansion.

The macro containing the contents is \glo@some_entry@name, in this case \glo@myglsentry@name. Because this macro contains the @ symbol you need \makeatletter and \makeatother.

MWE:

\documentclass{article}
\usepackage{glossaries-extra}
\makenoidxglossaries

\newglossaryentry{myglsentry}{name={Myglsentry},description={myglsentry desc}}

\newwrite\mytextfile \immediate\openout\mytextfile=\jobname.txt

\begin{document} \glsaddall \makeatletter \immediate\write\mytextfile{\unexpanded\expandafter{\glo@myglsentry@name}} \makeatother

\immediate\closeout\mytextfile

\printnoidxglossary \end{document}

Marijn
  • 37,699
  • Thanks for your answer. I thought it would be more easy to use... Indeed I need this for automaticly write gls entries "output" to a file. I tried to create a command like \renewcommand*{\glsname}[1]{\makeatletter\glo@#1@name\makeatother} but the use of \unexpanded make the #1 unexpanded ;) Do you think I shoud ask a new question for this? – zetyty Feb 18 '22 at 19:53
  • 1
    @SylvainRigal it's not the \unexpanded that's the problem there, it's the way of creating a macro name out of different pieces. The @ is nothing special, it's just a letter (because \makeatletter was used, i.e., make 'at' a letter). So it is similar to create for example \mycommandname out of \my and command and name. This is possible in LaTeX but you need the \csname construct for this (see https://tex.stackexchange.com/questions/39380/what-exactly-do-csname-and-endcsname-do for example). – Marijn Feb 19 '22 at 15:14
  • 1
    Also, \makeatletter and \makeatother need to be outside of the macro definition, they need to take effect when the macro is created, not when it is executed. However, with such a setup you would need two expansions, one to expand the (renewed) \glsname and one to expand \glo@xyz@name while \unexpanded\expandafter only performs one expansion - this is the whole reason to use the most low-level macro in the first place instead of a higher-level macro that calls a series of increasingly lower level macros. – Marijn Feb 19 '22 at 15:17
  • But I'm not sure why you can't just use \write\mytextfile{\unexpanded\expandafter{\glo@myglsentry@name}} and you want to use \write\mytextfile{\glsname{myglsentry}} instead? – Marijn Feb 19 '22 at 15:18
  • And about your last comment, it's hard to explain here but I need to write a "list" of gls entries outputs automatically, so I need to be able to automatically change the xyz in \glo@xyz@name. AND my gls entries could be defined with another entries like for e.g. the name field could be defined as: name={\glsname{anotherglsentrie} Myglsentry},. This is why I want to redefine the \glsname command (just for the purpose of writting to a file)... – zetyty Feb 19 '22 at 18:49
  • I sincerely thank you for your comments. I define this \makeatletter\renewcommand{\glsname}[1]{\csname glo@#1@name\endcsname}\makeatother and when I do this (without any unexpansion) \immediate\write\mytextfile{\glsname{myglsentry}} it well write the "output" of \glsname (i.e. Myglsentry) to the file !!! Do you know why it works without unexpansion? – zetyty Feb 19 '22 at 19:58
  • I wonder what are the low level macros names corresponding to all the \gls like commands (I posted a new question here for that). – zetyty Feb 20 '22 at 11:53