Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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/.
share|improve this question

3 Answers 3

\starttext
  \cldcontext {lfs.currentdir ()}
\stoptext
share|improve this answer

In ConTeXt:

\starttext

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

\stoptext
share|improve this answer
    
I'm assuming this is specific to the LuaTeX engine? –  Sean Allred Feb 23 at 18:49
1  
The question is tagged "context-mkiv," so yes, this is luatex. –  Thomas Feb 23 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 :) –  Sean Allred Feb 23 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. –  phg Feb 24 at 18:27

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}
share|improve this answer
    
Or like in Context, Latex users can just use Luatex: \directlua {tex.sprint (lfs.currentdir())}. –  phg Feb 23 at 10:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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