8

I am defining a new command, where the input contains backslash, which is to be removed later, hence I am searching for a way to accomplish that. The following example removes backslash when it is in-front of the string, but fails when the slash is in-between a string.

\documentclass{article}
\makeatletter
\newcommand{\removeabs}[1]{%
  \ifcat\relax\noexpand#1%
    \expandafter\expandafter\expandafter\@gobble\expandafter\string
  \fi
  #1%
}
\makeatother
\begin{document}
\removeabs{\the slash is removed here}
\removeabs{but not \here}
\end{document}

Does anybody know how to remove the backslash, even if it is in-between a string ?

Why I need this: I am using pythontex to create a_file.tex, which is to be included to the main.tex, and both steps are done using a single new command of form \command{parameters}, where the argument parameters are options to be used inside the pythoncode, as well as the name of the tex file created (a_file.tex). The issue comes when parameters contain slashes,hyphens etc (usual in my case), which are not even allowed as filenames in Windows OS, hence need to be stripped in pythontex code (to save as a file) and in latex (to read the file). An example would be:

\newcommand{\mycommand}[1]{
\begin{pycode}
plot[#1] %Pseudo line
save[#1.tex] %Pseudo line
\end{pycode}
\input{#1.tex}}
3
  • 1
    I find it hard to imagine an application for this.
    – egreg
    Oct 10, 2018 at 17:42
  • @egreg, I have added the requirement to the question.
    – Ashok
    Oct 10, 2018 at 19:01
  • 1
    I see; could you please add a typical example?
    – egreg
    Oct 10, 2018 at 19:06

2 Answers 2

9
\documentclass{article}
\newcommand{\removeabs}[1]{%
  %{\catcode92=9 \endlinechar-1 \scantokens\expandafter{\detokenize{#1}}}%
  {\catcode92=9 \endlinechar-1 \scantokens{#1}}%
}
\begin{document}
\removeabs{\the slash is removed here}
\removeabs{and also \here}+++
\end{document}

But your use case is not really known...

enter image description here

As pointed out by @A.Ellett there is no real need for \detokenize.

\documentclass{article}
\newcommand{\removeabs}[1]{%
  {\catcode92=9 \endlinechar-1 \scantokens{#1}}%
}
\begin{document}
\removeabs{\the slash $E=mc^2$ is removed here}
\removeabs{and also \here}+++
\end{document}

enter image description here

Perhaps you want to store in macro, the following shows how to do it (but the \x here is used and discarded).

Notice though that therein you should not be some active character with a definition not compatible with an \edef. The ~ and the UTF-8 things are OK.

\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand{\removeabs}[1]{%
  {\catcode92=9 \endlinechar-1 \everyeof{\noexpand}\edef\x{\scantokens{#1}}\x}%
}
\begin{document}
\removeabs{\the slash $E=mc^2$ is~~~~removed here}
\removeabs{and also éééé \here}+++
\end{document}

enter image description here

Thanks to @A.Ellett!

4
  • 2
    Is \detokenize really necessary there? Won't \scantokens alone handle everything?
    – A.Ellett
    Oct 10, 2018 at 15:33
  • Try \removeabs{\the@ slash} and you'll see a wrong space. The fact that \the is followed by a space (also in your example) is to be considered a misfeature in \scantokens.
    – egreg
    Oct 10, 2018 at 17:40
  • @egreg \scantokens inherits from TeX. Doing \toks0{\the@ slash} followed by a \write of \the\toks0 will, as you know well, produce \the<space>@ in file. But TeX could have decided that @ being of catcode 12, it should be \the@, assuming same catcode regime will get used for input as it was for output. That it does use \the<space>@ has its pros and cons (which I have not given much thought to) and they have I presume been thoroughly thought over by Knuth and he decided for the space, which \scantokens inherit.
    – user4686
    Oct 10, 2018 at 20:29
  • @egreg a much more serious "no-use" matter for \scantokens is when the space is \active. Then serious problems arise, due to these spaces always being appended to unexpanded control words when written to file. This means we end up with many active spaces where they were no spaces at all initially.
    – user4686
    Oct 10, 2018 at 20:31
4

With expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\removebs}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_all:nnN { \cC. } { \c{cs_to_str:N} \0 } \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }

\ExplSyntaxOff

\begin{document}

\removebs{\the slash is removed here}

\removebs{\the\ slashes are removed here}

\removebs{and also \here}

\end{document}

Note that the space after \the in the first case doesn't disappear, because it was ignored to begin with.

enter image description here

You must log in to answer this question.

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