7

Assume the file, source.tex is placed in /home/user/Birds/ and the user types context source.tex to compile the file. Is there a way for ConTeXt to retrieve the name of the folder that the file is located in (in this example, "Birds"), for use as a variable? This might be used, for example, to give the document a title, e.g. \title{\directoryname}.

  • This will only be used on a Linux system and needn't be compatible with other systems.
  • Directory names with spaces might appear, e.g. /home/user/Tropical\ Fish/.
Village
  • 13,603
  • 23
  • 116
  • 219

3 Answers3

7
\starttext
  \cldcontext {lfs.currentdir ()}
\stoptext
2

The \SetMacroToShellOutput defined below will set the given csname to the ouptut of the shell command that is provided to it. So

\SetMacroToShellOutput{\DirectoryNameWithPath}{pwd}

will set \DirectoryNameWithPath to the full Unix path of the current file name. You can use any Unix comamnds other than pwd and get the desired result. For instance

    \SetMacroToShellOutput{\FileNameWithoutPath}{basename `pwd`}

will set \FileNameWithoutPath to the current file name (without the path).

References:

Code:

\documentclass{article}

\makeatletter \newcommand\SetMacroToShellOutput[2]{% \begingroup\endlinechar=\m@ne\everyeof{\noexpand}% \edef\TempResult{\endgroup \expandafter\def\noexpand#1{% @@input|"#2" }}% \TempResult} \makeatother

\begin{document} \SetMacroToShellOutput{\DirectoryNameWithPath}{pwd}% DirectoryNameWithPath = \DirectoryNameWithPath \end{document}

Peter Grill
  • 223,288
2

In ConTeXt:

\starttext

\ctxlua{context(os.resultof("pwd"))}

\stoptext
Thomas
  • 1,726