# Evalution of new version command / Code review

I'm still newbie latex user, so I need help from advanced users to figure out in macros restrictions and their pitfalls

## Prerequisites:

I need to extract filename from its path, and I found this solution: https://tex.stackexchange.com/a/325052/272637. But it does not support passing command as parameter (a.e. \filename{\mycomm{param}}).

What happen if I call \filename{\mycomm{param}}? Assume, \mycomm{param} defined as

% NormalTeXSyntaxON
\def\setcase#1 {\expandafter\def\csname col:#1\endcsname}
\def\mycomm#1{\expandafter\ifx\csname col:#1\endcsname\relax \coldefault
\else \csname col:#1\endcsname\fi}

\def\coldefault {example/path.l}

% NormalTeXSyntaxOff


And \filename defined as in solution:

\makeatletter
\DeclareRobustCommand{\filename}[1]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#1}%
\edef\filename@base{\detokenize\expandafter{\filename@base}}%
\texttt{\filename@base.\filename@ext}%
\endgroup
}
\makeatother

• Actual result: filename{\mycomm{param}} prints example/path.l
• Expected result: path.l

## Proposed update:

I added support passing command as parameter, and tried to make this feature cross-platform. Also I added support for optional first parameter (i.e I can call \filname[\bfseries]{path} to make path bold).

New definition:

\newcommand{\runtimeFsep}{/}

\updateRuntimeFsep

\makeatletter
\DeclareRobustCommand{\filename}[2][]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#2}%
\IfSubStr{\filename@base}{\runtimeFsep}{\filename@parse{\filename@base}}{}%
\edef\filename@base{\detokenize\expandafter{\filename@base}}%
#1{\filename@base.\filename@ext}%
\endgroup
}
\makeatother


1. It has more dependencies:
\usepackage{xstring}
\usepackage{currfile}

1. It require pass '-recorder' option to LaTeX compiler because of currfile not work without it.

Minimal Working Example for old command:

\documentclass{letter}

% NormalTeXSyntaxON
\def\setcase#1 {\expandafter\def\csname col:#1\endcsname}
\def\mycomm#1{\expandafter\ifx\csname col:#1\endcsname\relax \coldefault
\else \csname col:#1\endcsname\fi}

% NormalTeXSyntaxOff

\makeatletter
\DeclareRobustCommand{\filename}[1]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#1}%
\edef\filename@base{\detokenize\expandafter{\filename@base}}%
\texttt{\filename@base.\filename@ext}%
\endgroup
}
\makeatother

\begin{document}
\filename{\mycomm{a}}
\end{document}


Minimal Working Example for new version of command:

\documentclass{article}

\usepackage{xstring}
\usepackage{currfile}

% NormalTeXSyntaxON
\def\setcase#1 {\expandafter\def\csname col:#1\endcsname}
\def\mycomm#1{\expandafter\ifx\csname col:#1\endcsname\relax \coldefault
\else \csname col:#1\endcsname\fi}

% NormalTeXSyntaxOff

\newcommand{\runtimeFsep}{/}

\updateRuntimeFsep

\makeatletter
\DeclareRobustCommand{\filename}[2][]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#2}%
\IfSubStr{\filename@base}{\runtimeFsep}{\filename@parse{\filename@base}}{}%
\edef\filename@base{\detokenize\expandafter{\filename@base}}%
#1{\filename@base.\filename@ext}%
\endgroup
}
\makeatother

\begin{document}
\filename{\mycomm{a}}
\end{document}


Is this good update? New version of this command has any pitfalls?

• Welcome to SE. Please don't post fragments of code. Gather the code into a Minimal Working Example so make things easier for the people willing to help. Jun 18 at 12:43
• So your code already works you just look for some code review? Jun 18 at 13:02
• \edef\filename@base{\detokenize will break any non ascii characters in the filename if you are using latex or pdflatex Jun 18 at 13:02
• @DavidCarlisle That part was originally copied from egreg's linked answer (although it looks reasonable either way) Jun 18 at 13:08
• @user202729 a detokenized name would work for file access commands, but if you want to print it, you need the inputenc active characters Jun 18 at 13:11