7

I was successful before in writing the hashtag symbol # into an output file. please see the following MWE.

\documentclass{book}
\usepackage{newverbs}

\Verbdef\myHashtagSymbolOne{#}%
\newcommand*{\myWriteNewLine}{^^J}%
\newwrite\myOutputFileName%

\begin{document}
\edef\myHashtagSymbolTwo{\myHashtagSymbolOne}%
\edef\myHashtagSymbolThree{\detokenize\expandafter{\myHashtagSymbolTwo}}%

\immediate\openout\myOutputFileName=hashtag.tex%
\immediate\write\myOutputFileName%
{%
Here is my hashtag:%
\myWriteNewLine%
\myHashtagSymbolThree%
}%
\immediate\closeout\myOutputFileName%

\end{document}

I compiled using pdflatex with shellescape (MikTeX), which is working fine.

Note that the output file hashtag.tex used to contain exactly the following:

Here is my hashtag:

In the MWE, we can see that the goal is for \myHashtagSymbolThree to appear in the output file. We also notice that there are "intermediate" variables such as \myHashtagSymbolOne and \myHashtagSymbolTwo, which are necessary for my actual (long) program to run (and these variables serve other purposes in my program, but are no longer shown here.)

Now, I updated my MikTeX program just a few days ago. I also updated all packages, including the newverbs package. For one reason or another, when I compiled my program, the following now appears in the output file hashtag.tex:

Here is my hashtag:

\protect #

Suddenly, the unnecessary command \protect suddenly appeared. This appearance is unacceptable to me because my other programs which would use the output file are no longer functioning properly because of the appearance of \protect. To verify indeed that \protect is causing the problem, i removed/deleted it temporarily from the output file, and my other programs which use the output file are now working properly. This leads me to conclude that \protect should not be there. But I am at a loss why it suddenly appeared. What I wanted is for only the hashtag to appear (without the \protect).

Seeking kindly your help.

3
  • 5
    Seeing “#” called the “hashtag symbol” makes me feel old and crotchety… Apr 5, 2021 at 10:14
  • 1
    it is an intentional change in the newverbs packages, it makes the commands robust now, but the documentation claims that they are not robust. Open an issue at the sourceforge site. Apr 5, 2021 at 10:45
  • @UlrikeFischer thank you for your comment. it confirms my suspicions that something changed in newverbs -- i just can't pinpoint what it is. it's the only logical explanation. it was working before, why would it not work now? Apr 5, 2021 at 16:51

2 Answers 2

7

It works if I use TeX Live prior to the 2020 release, which uses newverbs version 1.4 or earlier; with TeX Live 2020 or 2021 I get newverbs version 1.6 that uses a different method for \Verbdef:

version 1.6

\newcommand*\Verbdef{%
    \@ifstar
        {\@Verbdef*}%
        {\@Verbdef{}}%
}
\def\@Verbdef#1#2{%
    \Collectverb#1{\DeclareRobustCommand#2}%
}

version 1.4

\newcommand*\Verbdef{%
    \@ifstar
        {\@Verbdef*}%
        {\@Verbdef{}}%
}
\def\@Verbdef#1#2{%
    \Collectverb#1{\def#2}%
}

Why did Martin choose for \DeclareRobustCommand I don' know: we can just see

% \changes{v1.6}{2021/01/06}{Changed \cs{Verbdef} to define robust commands.}

in the dtx file.

On the other hand, you can simply use \string#:

\immediate\write\myOutputFileName{%
  Here is my hashtag:\myWriteNewLine
  \string#
}
\immediate\closeout\myOutputFileName

A different method for collecting verbatim material and storing it in to a “named variable”, which doesn't suffer from the problem:

\documentclass{book}

\ExplSyntaxOn

\NewDocumentCommand{\xverbdef}{mv}
 {
  \tl_clear_new:c { l_xverbdef_#1_tl }
  \tl_set:cn  { l_xverbdef_#1_tl } { #2 }
 }
\NewExpandableDocumentCommand{\useverb}{sm}
 {
  \IfBooleanTF{#1}
   { \tl_use:c { l_xverbdef_#2_tl } }
   { \useverbp { #2 } }
 }
\NewDocumentCommand{\useverbp}{m}
 {
  \tl_use:c { l_xverbdef_#1_tl }
 }

\ExplSyntaxOff

\xverbdef{hash}{#}

\newcommand*{\myWriteNewLine}{^^J}

\newwrite\myOutputFileName

\begin{document}

\tableofcontents

\section{\useverb{hash}}

\immediate\openout\myOutputFileName=\jobname-out.tex
\immediate\write\myOutputFileName{%
  Here is my hashtag:\myWriteNewLine
  \useverb*{hash}%
}
\immediate\closeout\myOutputFileName

We can also use the character elsewhere \useverb{hash}

\end{document}

When you want to use the “raw” contents, do \useverb*{<name>}.

The printed output

enter image description here

The file written out

Here is my hashtag:
#
4
  • thank you for your comment. it confirms my suspicions that something changed in newverbs -- i just can't pinpoint what it is. it's the only logical explanation. it was working before, why would it not work now? i tried using the \string, and it worked! thank you Apr 5, 2021 at 16:54
  • 1
    @beethovengg14 The idea of \Verbdef is to be able to use the so-defined macro for printing the contents verbatim; not for writing it into files. The package now makes the macro robust, so it can also be used in section titles or captions. The fact that it used to work for writing to files has always been incidental.
    – egreg
    Apr 5, 2021 at 17:27
  • 1
    @beethovengg14 I added a different approach.
    – egreg
    Apr 5, 2021 at 17:47
  • i think you have successfully generalized the problem, and the solution. thank you! - dennis Apr 6, 2021 at 3:33
5

This will work. In fact, \myHashtagSymbolOne as well as \myHashtagSymbolTwo will also output a lone #.

\documentclass{book}
%\usepackage{newverbs}

\edef\myHashtagSymbolOne{\string#}%
\newcommand*{\myWriteNewLine}{^^J}%
\newwrite\myOutputFileName%

\begin{document}
\edef\myHashtagSymbolTwo{\myHashtagSymbolOne}%
\def\myHashtagSymbolThree{\myHashtagSymbolTwo}%

\immediate\openout\myOutputFileName=hashtag.tex%
\immediate\write\myOutputFileName%
{%
Here is my hashtag:%
\myWriteNewLine%
\myHashtagSymbolThree%
}%
\immediate\closeout\myOutputFileName%

\end{document}
2
  • thank you for the answers! i will check them now if it works in my program... Apr 5, 2021 at 16:00
  • 1
    thank you steven. your suggested solution worked for me! Apr 5, 2021 at 16:53

You must log in to answer this question.

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