## LaTeX forum ⇒ LyX ⇒ Remove underscores from filename Topic is solved

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
bobred
Posts: 3
Joined: Fri Jul 15, 2016 4:05 pm

### Remove underscores from filename

Hi

I am using LyX 2.2 on Windows 7, MiKTeX is up to date. I am using currfile to get the file name and display it on the header. My problem is that it is displaying spaces with underscores. Example

what I have: RP-C-1_Strontium_constancy_checks
what I want: RP-C-1 Strontium constancy checks

Here is my MWE preamble, all other settings are as default

\usepackage[T1]{fontenc}\usepackage{fancyhdr}\pagestyle{fancy}\usepackage{babel}\usepackage[unicode=true,pdfusetitle,bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]{hyperref}\usepackage{currfile}\usepackage{xstring} \newcommand{\fileName}{\currfilebase} \pagestyle{fancy}\fancyhf{}\lhead{\StrSubstitute{\fileName}{"}{}}

I am using pdfLaTeX. If I export to LaTeX (pdflatex) from LyX and run it the underscores are not there?

Any ideas how I can get rid of the underscores?
Thanks
Last edited by cgnieder on Fri Jul 15, 2016 6:18 pm, edited 1 time in total.

Tags:

Johannes_B
Site Moderator
Posts: 4160
Joined: Thu Nov 01, 2012 4:08 pm
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm
It is important to know here that every character in LaTeX has a category code assigned. This means that an a might not be the same as an a, namely they are different if they have different category codes.

Say you have a file called test_test.tex and run the following code:

\documentclass{article}\usepackage{currfile}\usepackage{xstring} \begin{document} \StrSubstitute{\currfilebase}{e}{a}[\fileName]\show\fileName \end{document}

The reason the code above could work at all is that \StrSubstitute expands its arguments so this seems to be equal to
\StrSubstitute{test_test}{e}{a}[\fileName]

Except that we see in the log that no substitution has taken place:
> \fileName=macro:->test_test.l.8 \show\fileName

This is because all characters in \currfilebase have category code 12 (“other”) while the e in the second argument has category code 11 (“letter”). So we can use \string which assigns the character following it catcode 12:

\documentclass{article}\usepackage{currfile}\usepackage{xstring} \begin{document} \StrSubstitute{\currfilebase}{\string e}{a}[\fileName]\show\fileName \end{document}

Now we see:

> \fileName=macro:->tast_tast.l.8 \show\fileName

We can use the same strategy to replace _ (which usually has catcode 8 (“subscript”)) with a blank:

\documentclass{article}\usepackage{currfile}\usepackage{xstring} \begin{document} \StrSubstitute{\currfilebase}{\string_}{ }[\fileName]\show\fileName \end{document}

The log now says:

> \fileName=macro:->test test.l.8 \show\fileName

Regards
site moderator & package author

cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm
Johannes_B wrote:Crosspost

Strange: so you say (in comments to the crosspost) that your actual file does not have underscores in it? Then I don't understand why \currfilebase should have them…

Your MWE doesn't show them for a file with no _ in its name.
site moderator & package author

bobred
Posts: 3
Joined: Fri Jul 15, 2016 4:05 pm
Hi
Thank you cgnieder.
Worked a treat.

 \StrSubstitute{\currfilebase}{\string _}{ }[\fileName]

In TexNicCenter and TexWorks the filename is surrounded by quotes whereas in Lyx it is not.

James