如何排版文件路徑?

如何排版文件路徑?

我想正確排版檔案路徑,例如

C:\Program Files\Some program\bin\executable.exe

我遇到的第一個問題是 LaTeX 試圖將其視為\Program命令。我嘗試用​​另一個反斜線來轉義它,這會創建一個換行符。

然後我找到了這個包選單鍵但我並不是很高興,因為它將系統特定的分隔符號(即\Windows 和/類別 Unix)排版為一些奇怪的箭頭。

這引出了我的問題:在 LaTeX 中排版檔案路徑的首選方式是什麼?

答案1

url您可以使用選項載入套件obeyspaces並將完全限定的檔案名稱包含在\url指令中:

在此輸入影像描述

\documentclass{article}
\usepackage[obeyspaces]{url}
\begin{document}
\path{C:\Program Files\Some program\bin\executable.exe} % equivalent to \url{...}, but more semantic
\end{document}

附錄:如果您需要或希望允許在空格處換行,spaces也請選擇以下選項:

\usepackage[obeyspaces,spaces]{url}

答案2

我很喜歡menukeys,我認為你仍然應該考慮使用它。

不幸的是,menukeys目前沒有為用戶提供客製化的方法輸出路徑分隔符號(您所指的「奇怪的箭頭」)。不過,我確信托比亞斯‧韋()的作者menukeys如果看到這個問題,他會在未來的版本中添加該功能。

同時,這裡有一種自訂路徑分隔符號的方法。

在此輸入影像描述

\documentclass{article}

\usepackage{menukeys}

\makeatletter

% --- macro for changing path sep ---
\newcommand\setnewpathsep[1]
{%
    \tw@declare@style@simple*{paths}{%
       {\ttfamily\CurrentMenuElement}%
    }[%
       #1%
    ]{blacknwhite}
}

% --- reset the path separator (macro expands to original style def) ---
\newcommand\resetpathsep
{%
     \tw@declare@style@simple*{paths}{%
       {\ttfamily\CurrentMenuElement}%
    }[%
       \hspace{0.2em plus 0.1em}%
       \raisebox{0.08ex}{%
          \tikz{\fill[\usemenucolor{txt}] (0,0) -- (0.5ex,0.5ex)%
                    -- (0,1ex) -- cycle;}%
    }%
       \hspace{0.2em plus 0.1em}%
    ]{blacknwhite}
}

\makeatother

\begin{document}
% original style
\directory{C:/Program Files/Some program/bin/executable.exe}

% Windows style
\setnewpathsep{\textbackslash}
\directory{C:/Program Files/Some program/bin/executable.exe}

% Unix style
\setnewpathsep{/}
\directory{C:/Program Files/Some program/bin/executable.exe}

% back to original style
\resetpathsep
\directory{C:/Program Files/Some program/bin/executable.exe}
\end{document}

相關內容