LyXRemove underscores from filename

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

Remove underscores from filename

Post by bobred »

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

Code: Select all

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Remove underscores from filename

Post by Johannes_B »

The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Remove underscores from filename

Post by cgnieder »

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:

Code: Select all

\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

Code: Select all

\StrSubstitute{test_test}{e}{a}[\fileName]
Except that we see in the log that no substitution has taken place:

Code: Select all

> \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:

Code: Select all

\documentclass{article}
\usepackage{currfile}
\usepackage{xstring}

\begin{document}

\StrSubstitute{\currfilebase}{\string e}{a}[\fileName]
\show\fileName

\end{document}
Now we see:

Code: Select all

> \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:

Code: Select all

\documentclass{article}
\usepackage{currfile}
\usepackage{xstring}

\begin{document}

\StrSubstitute{\currfilebase}{\string_}{ }[\fileName]
\show\fileName

\end{document}
The log now says:

Code: Select all

> \fileName=macro:
->test test.
l.8 \show\fileName
Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Remove underscores from filename

Post by cgnieder »

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

Remove underscores from filename

Post by bobred »

Hi
Thank you cgnieder.
Worked a treat.

Code: Select all

 \StrSubstitute{\currfilebase}{\string _}{ }[\fileName]
In TexNicCenter and TexWorks the filename is surrounded by quotes whereas in Lyx it is not.

James
Post Reply