13

I'm fairly new to LaTeX and I'm struggling with something apparently simple, but for which I can't find a fair solution. The problem (simplified) is as follows (please take care terms used may not have the traditional meaning in LaTeX):

I need to references to files (many) throughout a text. As example, the files: "C:/Work/this_file1.c", "C:/MoreWork/this_file1.c" and "D:/MoreWork/another_file2.h".

So, now I would like to create an "alias" to each directory and each name file so I can reference them. A "pseudocode" could be:

% Definition
\alias \c_dir = C:/Work/
\alias \d_dir = D:/MoreWork/
\alias \file1 = this_file1.c
\alias \file2 = another_file2.h

% Reference in text
\c_dir\file1    % For referencing C:/Work/this_file1.c
\d_dir\file1    % For referencing D:/MoreWork/this_file1.c
\c_dir\file2    % For referencing C:/Work/another_file2.h

My first approach was using \newcommand, but that caused numerous problems due to naming limitations. (I also tried other methods like pgfkeys, but it always gets stuck somewhere!)

Can you suggest a method?

  • just use \newcommand while just using letters for the name. – David Carlisle Nov 15 '16 at 14:13
  • Welcome to TeX.SX. Dont forget to mark a solution. Very good first question by the way. Very easy to understand, clear expectation. – MaestroGlanz Nov 15 '16 at 14:46
18

For more flexible alias names, you can do it like this. In the examples I removed the underscore; depending on the intended usage you may want to escape them (\_) or not (_).

\documentclass{article}
\newcommand\alias[2]{\expandafter\def\csname alias:#1\endcsname{#2}}
\newcommand\A[1]{\csname alias:#1\endcsname}

\alias{c_dir}{C:/Work/}
\alias{d_dir}{D:/MoreWork/}
\alias{file1}{thisFile1.c}
\alias{file2}{anotherFile2.h}

\begin{document}
\A{c_dir}\A{file1}

\A{d_dir}\A{file1}

\A{c_dir}\A{file2}
\end{document}

As suggested by @MaestroGlanz, the commands \alias and \A can be defined more compactly using the package etoolbox:

\usepackage{etoolbox}
\newcommand\alias[2]{\csdef{alias:#1}{#2}}
\newcommand\A[1]{\csuse{alias:#1}}

Because mentioned in the original question, here a pgfkeys solution (though I'm not aware of any advantage):

\usepackage{pgfkeys}
\newcommand\alias[2]{\pgfkeyssetvalue{/alias/#1}{#2}}
\newcommand\A[1]{\pgfkeysvalueof{/alias/#1}}

The result of the sample document in all versions:

enter image description here

  • You should add a version with \csdef from the etoolbox. This is more understandable for a rookie. – MaestroGlanz Nov 15 '16 at 14:45
  • @MaestroGlanz Thanks for the suggestion, added. – gernot Nov 15 '16 at 14:56
14

You can just use \newcommand, so long as the command names obey tex rules (consist of letters, essentially).

% Definition
\newcommand\cdir{C:/Work/}
\newcommand\ddir{D:/MoreWork/}
\newcommand\fileA{this\_file1.c}
\newcommand\fileB{another\_file2.h}

% Reference in text
\cdir\fileA    % For referencing C:/Work/this_file1.c
\ddir\fileA    % For referencing D:/MoreWork/this_file1.c
\cdir\fileB    % For referencing C:/Work/another_file2.h
0

Thank for your replies... that were quite useful... and made me go a little further.

First, the initial question is not as clear as should be, so a more 'real' example should do the trick:

In the LaTeX document I need to "include" parts of external files (typically software source code). The source files need to be copied to a temporary location and (Important) get a temporary name (something like temp1.tmp, temp2.tmp... and so on). [Note: how the copy is made is of no concern... just that the paths and filenames must be tracked.]

So, below I have the solution that I'm using. Just sharing...

\documentclass{article}

\usepackage{keyval}
\makeatletter
\newcounter{cntTmp}
\newcommand{\mylongS}[4]{
    % In here I pass the source and destination file parameters
    srcDir: #1; srcFile: #2; dstDir: #3; dstFile: #4    

    %I Now use the parameters to create a string to store in a just-defined key
    \define@key{}{this\arabic{cntTmp}}[]{#1:#2:#3:#4}
    \stepcounter{cntTmp}
}

\makeatother

\begin{document}
    % This should "account" as the first file copied... to temp0.tmp    
    \mylongS{S:/work}{testtxt}{t}{temp0.tmp}

    % This should "account" as the SECOND file copied... to temp1.tmp   
    \mylongS{S:/work}{testtxt}{t}{temp1.tmp}

    % Now I have access to the full string... and may use string manipulation
    \setkeys{}{this0}

    \setkeys{}{this1}
\end{document}

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.