3
$\begingroup$

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,

CalculusI/temp.nb

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

 Cell[StyleData["Notebook"],
 PageFooters->{{None, 
    FileNameSplit[NotebookDirectory[]][[-1]], None}, {None, 
    None, None}},
 PrintingOptions->{"FacingPages"->True,
 "FirstPageFace"->Right,
 "FirstPageFooter"->True,
 "FirstPageHeader"->False}]

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.

CurrentValue["NotebookFileName"]

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

$\endgroup$
3
$\begingroup$

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:

Cell[StyleData["Notebook"],
    PageFooters->{
        {
        None,
        Cell[
            BoxData @ DynamicBox @ ToBoxes @ FileNameTake @ CurrentValue["NotebookDirectory"],
            ShowStringCharacters->False
        ],
        None
        },
        {None, None, None}
    }
]
$\endgroup$
0
$\begingroup$

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

NotebookFileName[]

Gives you:

C:\Users\Cody\Desktop\directorymain\subdirectory\targetnb.nb

And so:

FileNameSplit[NotebookFileName[]][[-2]]

Gives you:

subdirectory

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.

$\endgroup$
  • $\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$ – Michael McCain Apr 18 '19 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$ – CA Trevillian Apr 18 '19 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$ – Michael McCain Apr 20 '19 at 6:43
  • $\begingroup$ @MichaelMcCain and, to be clear, it would print just "temp.nb"? Or "Calculusl/temp.nb"? $\endgroup$ – CA Trevillian Apr 20 '19 at 6:46
  • $\begingroup$ Yes... using CurrentValue["NotebookFileName"], I get temp.nb as the footer when I print to PDF. $\endgroup$ – Michael McCain Apr 20 '19 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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