
私は LaTeX を使い始めたばかりで、一見単純な問題に苦労していますが、適切な解決策が見つかりません。問題 (簡略化) は次のとおりです (使用されている用語は LaTeX の従来の意味とは異なる可能性があるので注意してください)。
ファイルへの参照が必要です(多くの) をテキスト全体に含めます。たとえば、ファイル:
"C:/Work/this_file1.c"
、"C:/MoreWork/this_file1.c"
および"D:/MoreWork/another_file2.h"
。
そこで、各ディレクトリと各名前ファイルに「エイリアス」を作成して、それらを参照できるようにしたいと思います。「疑似コード」は次のようになります。
% 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
私の最初のアプローチは を使用することでした\newcommand
が、命名制限のために多くの問題が発生しました。( などの他の方法も試しましたpgfkeys
が、常にどこかで停止してしまいます。)
方法を提案してもらえますか?
答え1
より柔軟なエイリアス名にするには、次のようにします。例ではアンダースコアを削除しましたが、使用目的に応じて、アンダースコアをエスケープするか ( \_
)、しないか ( _
) を選択できます。
\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}
@MaestroGlanz の提案どおり、コマンド\alias
とコマンドは、\A
パッケージを使用してよりコンパクトに定義できますetoolbox
。
\usepackage{etoolbox}
\newcommand\alias[2]{\csdef{alias:#1}{#2}}
\newcommand\A[1]{\csuse{alias:#1}}
元の質問で言及されているので、ここにpgfkeys
解決策があります(ただし、利点はわかりません)。
\usepackage{pgfkeys}
\newcommand\alias[2]{\pgfkeyssetvalue{/alias/#1}{#2}}
\newcommand\A[1]{\pgfkeysvalueof{/alias/#1}}
すべてのバージョンのサンプル ドキュメントの結果:
答え2
\newcommand
コマンド名が TEX ルール (基本的には文字で構成されている) に従っている限り、を使用できます。
% 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
答え3
ご回答ありがとうございます。非常に役立ち、さらに前進することができました。
まず、最初の質問は明確ではないので、より「現実的な」例を挙げた方がよいでしょう。
LaTeX ドキュメントでは、外部ファイル (通常はソフトウェア ソース コード) の一部を「含める」必要があります。ソース ファイルは一時的な場所にコピーし、(重要) 一時的な名前 ( 、temp1.tmp
...temp2.tmp
など) を取得する必要があります。[注: コピーの作成方法は重要ではありません。パスとファイル名を追跡する必要があるだけです。]
以下に私が使用している解決策を示します。ただ共有するだけです...
\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}