使用 \newcommand 從子資料夾載入文字檔案時新增大括號

使用 \newcommand 從子資料夾載入文字檔案時新增大括號

使用 \newcommand 在子資料夾中載入文字檔案時會新增大括號。我在 bash 中創建了一些文件:

$ mkdir mydir
$ echo "This text is in mydir" > mydir/text.txt
$ echo "This text is outside mydir" > text.txt
$ tree
.
├── mydir
│   └── text.txt
├── test.tex
└── text.txt

2 directories, 3 files
$ cat -n test.tex        
     1  \documentclass[12pt]{article}
     2  
     3  \newcommand\inputtexta[1]{\input{{#1}.txt}}
     4  \newcommand\inputtextb[1]{\input{mydir/{#1}.txt}}
     5  
     6  \begin{document}
     7  
     8  \inputtexta{text}  % okay
     9  
    10  \inputtextb{text} % ! LaTeX Error: File `mydir/{text}.txt' not found.
    11  
    12  \input{mydir/text.txt}  % okay
    13  
    14  \end{document}

當我跑步時

$ pdflatex test.tex 
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023/Debian) (preloaded format=pdflatex)

...

! LaTeX Error: File `mydir/{text}.txt' not found.

請注意 {text} 周圍的大括號,`mydir/{text}.txt' 不存在...

謝謝你的幫忙!

相關內容