다양한 위치에서 파일 찾기

다양한 위치에서 파일 찾기

내 홈 폴더에 중앙 BibTeX 파일이 있습니다. 가끔 하위 폴더의 git 하위 모듈로 사용합니다 central-bibtex. 상대적인 위치는 약간 다를 수 있습니다. 내 서문이 나를 위해 작업을 수행하기를 원하므로 몇 가지 가능한 위치에서 이를 찾으려면 서문이 필요합니다. 여기에는 다음이 포함됩니다.

  • ../../central-bibtex/Central
  • central-bibtex/Central
  • ../../zentrale_BibTeX/Central

이에 대한 더 나은 구문이 있으면 더 많은 것을 추가하고 싶습니다.

  • ../central-bibtex/Central
  • ../zentrale_BibTeX/Central
  • ../../../zentrale_BibTeX/Central

지금까지 작동하는 것 같습니다.

\IfFileExists{../../central-bibtex/Central}
{\newcommand{\bibliographyfile}{../../central-bibtex/Central}}
{
    \IfFileExists{central-bibtex/Central}
    {\newcommand{\bibliographyfile}{central-bibtex/Central}}
    {\newcommand{\bibliographyfile}{../../zentrale_BibTeX/Central}}
}

이를 수행하는 쉬운 방법이 있습니까? 나는 Python으로 생각하고 있습니다 :

dirs = ['../../central-bibtex', 'central-bibtex', '../../zentrale_BibTeX']
for dir in dirs:
    if os.path.isfile(dir + '/Central'):
        bibliographyfile = dir + '/Central'

\foreach나는 TikZ 및 을 시도했지만 \bibliographyfile다음과 같이 아무것도 설정되지 않았습니다.

\newcommand\bibliographyfile{None}

\foreach \path in {
    ../../central-bibtex/Central,
    central-bibtex/Central,
    ../../zentrale_BibTeX/Central,
    /home/mu/Dokumente/Studium/zentrale_BibTeX/Central.bib
}
{
    \AtEndDocument{\path}
    \IfFileExists{\path}
    {\renewcommand\bibliographyfile{\path}}
    {}
}

답변1

루프 변수는 루프를 유지하지 못 하므로 \foreach저장하려는 값을 확장해야 합니다. 아래에서는 \xdef이를 수행하기 위해 an을 사용합니다.

여기에 이미지 설명을 입력하세요

노트:

  • 일부 문제가 발생할 수 있으므로 경로에서 밑줄을 제거했습니다. 하지만 이 사이트에는 도움이 되는 해결 방법이 있을 것입니다.

암호:

\documentclass{article}
\usepackage{tikz}

\newcommand\bibliographyfile{}

\foreach \FileNameWithPath in {
    ../central-bibtex/Central.bib,
    central-bibtex/Central.bib,
    ../../zentrale-BibTeX/Central.bib,
    /home/mu/Dokumente/Studium/zentrale-BibTeX/Central.bib
}{%
    %\AtEndDocument{\path}% <-- not sure what this was for
    \IfFileExists{\FileNameWithPath}{%
        %\xdef\bibliographyfile{\FileNameWithPath}%
        % Per egreg's suggestions, replaced the above with:
        \expandafter\gdef\expandafter\bibliographyfile\expandafter{\FileNameWithPath}
    }{}%
}

\begin{document}

Located file: \bibliographyfile

\end{document}

관련 정보