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/.

3 Answers 3

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

In ConTeXt:

\starttext

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

\stoptext
4
  • I'm assuming this is specific to the LuaTeX engine? Commented Feb 23, 2014 at 18:49
  • 1
    The question is tagged "context-mkiv," so yes, this is luatex.
    – Thomas
    Commented Feb 23, 2014 at 21:05
  • Ah; I'm not terribly familiar with either ConTeXt or LuaTeX, but I understood ConTeXt to be just a format—didn't know MkIV was LuaTeX-specific :) Commented Feb 23, 2014 at 23:15
  • 1
    @Sean Allred: If you’re curious about Context’s Lua integration, I can recommend mk.pdf, hybrid.pdf and the lists status-files.pdf as well as status-lua.pdf. Commented Feb 24, 2014 at 18:27
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}
1
  • Or like in Context, Latex users can just use Luatex: \directlua {tex.sprint (lfs.currentdir())}. Commented Feb 23, 2014 at 10:06

You must log in to answer this question.

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