LaTeX forum ⇒ LyXRemove 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

Postby bobred » Fri Jul 15, 2016 4:19 pm

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

  1. \usepackage[T1]{fontenc}
  2. \usepackage{fancyhdr}
  3. \pagestyle{fancy}
  4. \usepackage{babel}
  5. \usepackage[unicode=true,pdfusetitle,
  6. bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
  7. breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
  8. {hyperref}
  9. \usepackage{currfile}
  10. \usepackage{xstring}
  11.  
  12. \newcommand{\fileName}{\currfilebase}
  13.  
  14. \pagestyle{fancy}
  15. \fancyhf{}
  16. \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.
Reason: added code markup

Tags:

User avatar
Johannes_B
Site Moderator
Posts: 4074
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Fri Jul 15, 2016 6:33 pm

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: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Jul 15, 2016 6:39 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:

  1. \documentclass{article}
  2. \usepackage{currfile}
  3. \usepackage{xstring}
  4.  
  5. \begin{document}
  6.  
  7. \StrSubstitute{\currfilebase}{e}{a}[\fileName]
  8. \show\fileName
  9.  
  10. \end{document}


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

Except that we see in the log that no substitution has taken place:
  1. > \fileName=macro:
  2. ->test_test.
  3. 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:

  1. \documentclass{article}
  2. \usepackage{currfile}
  3. \usepackage{xstring}
  4.  
  5. \begin{document}
  6.  
  7. \StrSubstitute{\currfilebase}{\string e}{a}[\fileName]
  8. \show\fileName
  9.  
  10. \end{document}


Now we see:

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


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

  1. \documentclass{article}
  2. \usepackage{currfile}
  3. \usepackage{xstring}
  4.  
  5. \begin{document}
  6.  
  7. \StrSubstitute{\currfilebase}{\string_}{ }[\fileName]
  8. \show\fileName
  9.  
  10. \end{document}


The log now says:

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


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Jul 15, 2016 6:48 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.
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

bobred
Posts: 3
Joined: Fri Jul 15, 2016 4:05 pm

Postby bobred » Mon Jul 18, 2016 10:06 am

Hi
Thank you cgnieder.
Worked a treat.

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


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

James


Return to “LyX”

Who is online

Users browsing this forum: No registered users and 16 guests