[Tex/LaTex] How to trim .tex for each filename read from an external file

external filesprogramming

I have an external text file that contains a list of names of TeX input files. The names have trailing extensions .tex.
For example, the external text file named data.txt contains the following list:

a.tex
b.tex
c.tex

There is a PDF file for each TeX input file in the same directory in which data.txt exists.

The following codes are expected to iterate the file name without extension and import the corresponding PDF.

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\newread\reader
\immediate\openin\reader=data.txt\relax
\loop
    \read\reader to \x
    \unless\ifeof\reader
        \includegraphics{\x}\par
\repeat
\immediate\closein\reader
\end{document}

Could you help me to implement the trimming macro to remove .tex in \x?

Best Answer

The LaTeX2e kernel, provides some commands for parsing paths and extensions. It is preferable to use them for this type of parsing.

The macro \filename@parse scans its argument and splits it into three parts which are then saved in the macros \filename@area, \filename@base, \filename@ext

\documentclass{article}
\begin{document}
\makeatletter
\filename@parse{path/name.tex}
\filename@area, \filename@base, \filename@ext
\makeatother
\end{document}

In your case \filename@base will represent the trimmed filename, with no path or extension information.

Related Question