나중에 사용하기 위해 문자열과 같은 값을 저장하는 가장 좋은 방법은 무엇입니까?

나중에 사용하기 위해 문자열과 같은 값을 저장하는 가장 좋은 방법은 무엇입니까?

저는 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}

관련 정보