I am trying to use a stylesheet to create a footer that will use the directory name of the current file. For example, consider the following temp.nb file with stored within the CalculusI directory. That is,


I would like the footer to simply print out CalculusI. I have tried the following to no avail. Any ideas?

    FileNameSplit[NotebookDirectory[]][[-1]], None}, {None, 
    None, None}},

I can succesfully get the filename as the footer by using the following in the stylesheet replacing FileNameSplit[NotebookDirectory[]][[-1]] in my code above with the following.


However, I can't figure out how to grab the directory name (CalculusI) for the file and use it as the footer. Any ideas?


2 Answers 2


The main problem is that you must use boxes in the PageFooters option setting. A typical boxes expression consists of *Box objects with lists and strings. For example:

ToBoxes[f[x, "y"]]

RowBox[{"f", "[", RowBox[{"x", ",", "\"y\""}], "]"}]

CurrentValue is special, because it gets evaluated by the FrontEnd, and it evaluates to a string. This is why you can use CurrentValue in the option setting. However, adding FileNameSplit means the FrontEnd can't evaluate it, so it is an expression and doesn't get rendered properly.

So, use a Cell object instead. For example, the following returns the desired directory:

Dynamic @ FileNameTake @ CurrentValue["NotebookDirectory"]

The corresponding Cell expression is:

Cell @ BoxData @ ToBoxes @ Dynamic @ FileNameTake @ CurrentValue["NotebookDirectory"]

Cell[BoxData[ DynamicBox[ ToBoxes[FileNameTake[CurrentValue["NotebookDirectory"]], StandardForm]]]]

The following style should do what you want:

            BoxData @ DynamicBox @ ToBoxes @ FileNameTake @ CurrentValue["NotebookDirectory"],
        {None, None, None}

You almost had it!! I overcompplicated this initially in my answer, but just change "-1" to "-2" as in:


Gives you:


And so:


Gives you:


As output! Relevant references:

https://reference.wolfram.com/language/tutorial/NamingAndFindingFiles.html https://reference.wolfram.com/language/ref/FileNameTake.html

Hope this helps!! Also of value: https://reference.wolfram.com/language/workflow/ConstructAFilePathProgrammatically.html

The workflow pages are some of my more favorite ones to review for strategies.

  • $\begingroup$ I’m sorry. I should have been more clear. My problem isn’t how to get the name of the directory. Instead it is how to maybe it appear as a footer using a Stylesheet. $\endgroup$
    – B flat
    Apr 18, 2019 at 19:56
  • $\begingroup$ @MichaelMcCain you would just replace what you reference in your question, I am on mobile currently and can test it in a bit, but: "I can succesfully get the filename as the footer by using the following in the stylesheet replacing FileNameSplit[NotebookDirectory[]][[-1]] in my code above with the following." So it should work if you replace that with FileNameSplit[NotebookFileName[]][[-2]] --If my understanding is correct. I can edit my answer when I get to a PC $\endgroup$ Apr 18, 2019 at 20:39
  • $\begingroup$ Hello. I tried putting this direclty into the stylesheet. For some reason it doesn't like the call. It does work with CurrentValue["NotebookFileName"], but I don't want the file name. I can't seem to find a similair way using CurrentValue to get the directory name. $\endgroup$
    – B flat
    Apr 20, 2019 at 6:43
  • $\begingroup$ @MichaelMcCain and, to be clear, it would print just "temp.nb"? Or "Calculusl/temp.nb"? $\endgroup$ Apr 20, 2019 at 6:46
  • $\begingroup$ Yes... using CurrentValue["NotebookFileName"], I get temp.nb as the footer when I print to PDF. $\endgroup$
    – B flat
    Apr 20, 2019 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.