I am trying to automatically add section label based on the file name. My structure is as such:

  • main.tex
  • 1.0.introduction.tex
  • 1.1.0.foo.tex
  • 1.1.1.bar.tex

I am using \currfilename from package currfile to get the filename {which removes the .tex} and xstring to remove everything before the last {.} up to three levels deep.

So my newcommand clearname is producing the expected result: from 1.0.introduction -> introduction. The problem occurs when trying to add my command to the label.

While \label{\currfilename} works and the label gets created, when doing \label{\clearname{\currfilename}} I get 6 different compilation errors which I am too of a novice to wrap my head around.








% \label{\clearname{\currfilename}}

As clearly seen in \ref{1.0.introduction}

So to summarize I need the label created to be introduction and not 1.0.introduction.

enter image description here

  • Welcome to TexSE :) // Sorry, may be I miss your point. Don't you specify the section to be "Introduction" manually? Didn't you want s.th. like \label{\clearname ...} ?
    – MS-SPO
    Feb 15, 2022 at 12:00
  • Thanks for the welcome! There's hundreds of files representing sections, subsections etc which already have the correct label I would like to use. The section names on the other hand are not as good a match for labels as they can be a couple words. I'd rather not spend the time to manually assign labels whose names I'd like to use are already laying around anyway.
    – eliasf
    Feb 15, 2022 at 12:32
  • So yes, I would like to use \label{\clearname{\currfilename}} which is not working. I suspect it's about xstring not allowing expansion of the command inside \label but it supersedes my latex skills to "define \clearname globally". I have tried Mr. Carlisle's suggestion from here but to no avail.
    – eliasf
    Feb 15, 2022 at 12:44

1 Answer 1


Well... After spending a few hours on the very detailed and intricate xstring documentation... In order to get the expansion of xstring macros you have to pass an argument to the output such as: [\myresult].

I also realized what I need can easily be achieved by \StrGobbleLeft:

So to elaborate the below code works as expected:


...and produces the correct result by calling \label{\myresult} although you still need to run \clearname{\currfilename} before.

  • Congratulations :)
    – MS-SPO
    Feb 15, 2022 at 13:30
  • Thanks, I was already using the solution witihn the \clearname declaration anyway with the \tmp, I just hadn't understood it was needed for the output as well. Answering my first question on SE on my own kind of defeats the purpose but hey... Oh well..
    – eliasf
    Feb 15, 2022 at 13:40
  • I agree, xstring's documentation is challenging. A few suggestions to find faster what you need. // 0) Posting a question here is a valid approach ;-) // 1) Sometimes in the column to the right "Latest" or "Related" can give a clue. // 2) Upper left, try a Tag-search. I sometimes use it to get a quick overview about a package, its features and troubles. // 3) Try a google search like xstring extract and similar. // Purpose is getting clues, not detailed reading. // Anyway ... you made it :)
    – MS-SPO
    Feb 15, 2022 at 13:46
  • 1
    Thanks for your input once again. I had already done all the steps you mention and had spent around 5 hours on reading similar SE questions before deciding to post and finally tackle the daunting manual. Maybe just writing it down as a question for others to understand cleared things up for me.
    – eliasf
    Feb 15, 2022 at 15:36

You must log in to answer this question.

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