7

I want to use the menukeys package to typeset directories and filenames, but if a filename contains a hyphen, the output adds a space before the hyphen,eg

\documentclass{article}
\usepackage{menukeys}
\renewmenumacro{\directory}[bslash]{paths}
\begin{document}
Type \directory{C:\Users\username\AppData\Roaming\.emacs.d\site-lisp}
\end{document}

Is there a way to avoid this?Example output

5
  • As this bug persists, did you send a mail to the maintainer (or ping him here)? – TeXnician Jun 27 '17 at 11:28
  • No, I had forgotten. Thanks for reminding me; I just sent him email. – Peter Flynn Jun 28 '17 at 22:58
  • Tobi mailed me back to say he's in the middle of his thesis and stuck for time. So I'm going to have a look myself. – Peter Flynn Jun 30 '17 at 20:19
  • Any news here ? – Bobyandbob May 23 '18 at 13:56
  • Disclaimer: Currently I'm one of the two maintainers of menukeys. This won't be fixed and in future versions the bslash syntax will most likely be dropped entirely. I've provided some reasoning at the issue-thread on Github. Basically this can't be correctly parsed in a foolproof (and backwards compatible) manner, so there is no real point in trying, imho. – Skillmon Jan 2 at 18:28
1

If the separator character is bslash then the menukeys package uses \detokenize to parse the input. This is needed to prevent interpretation of \ characters as starting a control sequence (e.g., \Users\username could be interpreted as the LaTeX command \Users followed by the command \username).

However, \detokenize adds a space at the end of each control sequence (see What are the exact semantics of \detokenize? and Detokenizing without extra spaces?). Normally this is not a problem in menukeys, because the space is added at the end of a 'word', where either a space is needed (for paths with spaces) or where it is discarded at the end of the current directory name when printing the separator character.

An exception is a directory name that is not a valid control sequence name, such as \site-lisp. Here \detokenize parses \site, adds an extra space because it is at the end of a control sequence, and continues with -lisp.

The answer https://tex.stackexchange.com/a/45292/ from the second related question mentioned above notes an alternative for \detokenize, which is the v argument specifier in expl3. Such an argument is read verbatim and converted into character codes 10 (space) and 12 (other), which means that \ will not be interpreted as the start of a control sequence. This specifier therefore removes the need for \detokenize. Further processing in menukeys, specifically splitting the argument by the separator character with \seq_set_split, does not seem to be affected. Normal spaces in path names still work. Note however that I did not extensively test for any side effects.

To properly implement this modification the source code of the package should be changed (if the package maintainer approves of course). I added a comment at the existing Github issue https://github.com/tweh/menukeys/issues/48. However, as a temporary fix you can redefine the \directory macro in your own document, with the v specifier, in which the occurrences of \detokenize and \unexpanded are removed. Note that the bslash default value is hardcoded in the MWE below (the redefinition does not follow the \renewmenumacro command - but you can still provide the optional argument if necessary).


Update: in the Github issue the package maintainer stated that there would be too many side effects for this approach (for example that you cannot use actual macros in the path name anymore). He intends to drop support for backslash-delimited paths altogether in future releases of menukeys. So if you want to properly format this type of paths (and you don't mind about not using macros in the paths and not nesting menukeys macros) then you can use the code below.


MWE:

\documentclass{article}
\usepackage{menukeys}
\renewmenumacro{\directory}[bslash]{paths}
\makeatletter
\RenewDocumentCommand\directory{+O{bslash}+v}%           
{%
\leavevmode
\begingroup
  \def\tw@current@color@theme
    {\csname tw@style@paths@color@theme\endcsname}%
  \@nameuse{tw@style@paths@pre}%
  \tw@mk@test@input@sep{#1}
    {%
      \edef\tw@menu@list{#2}%
      \edef\tw@mk@tempa{\@backslashchar}%
    }
    {%
      \edef\tw@menu@list{#2}%
      \edef\tw@mk@tempa{\tw@mk@trimspaces{#1}}%
    }%
  \begingroup
    \letcs{\tw@mk@tempb}{tw@parse@menu@list@directory}%
    \tw@mk@indrisloop\tw@mk@tempa\tw@menu@list\tw@mk@tempb
  \endgroup
  \@nameuse{tw@style@paths@post}%
\endgroup
}%
\makeatother
\begin{document}

\noindent Type \directory{C:\Users\username\AppData\Roaming\.emacs.d\site-lisp}\\
Type \directory[/]{C:/Users/username/AppData/Roaming/.emacs.d/site-lisp}\\
Type \directory{C:\Users\[your user name]\AppData\Roaming\.emacs.d\site-lisp}   
\end{document}

Result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.