使用 \usepackage[dutch]{babel} 时,列表中使用带空格的路径会失败

使用 \usepackage[dutch]{babel} 时,列表中使用带空格的路径会失败

编辑:我刚刚发现这不是由于空格而是由于“”: {"E:/Cloud storage/GitHub/JS_PCI/afronden2.js"}如果 Babel 被禁用,这一行就会起作用,而这一行:{E:/Cloud storage/GitHub/JS_PCI/afronden2.js}即使 Babel 处于活动状态也会起作用。

我有一个奇怪的问题。

我使用 listings 来美化打印一些 JavaScript 文件。如果它们位于没有空格的文件夹中,则没有问题。但是如果路径中有空格,它们或多或少会默默失败(我确实得到了 PDF,但文本中缺少 Javascript)。

我发现 Babel 包不知为何导致了这个问题。如果我取消注释\usepackage[dutch]{babel}下面代码中的这一行,编译后“代码 2”就会消失。

\documentclass[12pt]{report} % Default font size is 12pt, it can be changed here
%\usepackage[dutch]{babel} % Het pakket babel zorgt ervoor dat al de benamingen in het Nederlands zijn
\usepackage{geometry} % Required to change the page size to A4


\geometry{a4paper, twoside} % Set the page size to be A4 as opposed to the default US Letter


\usepackage{listings}
\usepackage{cite}
\usepackage{caption}   %http://www.ctex.org/documents/packages/float/caption.pdf
\usepackage{upquote}
\usepackage{xcolor}
\usepackage{xcolor}
\usepackage{courier}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\renewcommand{\lstlistingname}{Code}

\lstset{
%language=Java,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
identifierstyle=\ttfamily,
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
showstringspaces=false,
basicstyle=\scriptsize\ttfamily,
tabsize=2,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
breakatwhitespace=false,
aboveskip={1\baselineskip},
columns=fixed,
upquote=true,
extendedchars=true,
frame=bottomline,
inputencoding=utf8
}

\lstset{emph={var,alert,Number,function}, emphstyle=\color{blue},
emph={[2]
    AddAllVisDigInfo,
    AddCallbackReference,
    AddCmdModToOperation,
    AddDigInfo,
    AddEntNameDig,
    AddEntNoDig,
    AddEntNameTopologyDig,
    AddEntNoTopologyDig,
    Addfreedig,
    AddMessageString,
    AddUserModToOperation,
    AddValidStateCmdMod,
    AddValidStateuserMod,
    Alias,
    AliasModalVar,
    Ang,
    Ask,
    AskBox,
    AskDig,
    AskDigInfo,
    AskDigInfoEx,
    AskMods,
    AskProfile,
    AskSurfaces,
    Benchmark,
    ChainEntName,
    ChainEntNo,
    ClearMods,
    CommitOperation,
    CopyModifiers,
    DebugLevel,
    DeleteAllVars,
    DoOperatationMods,
    ExecCommand,
    ExecCommandEx,
    FreeCommand,
    FreeDigInfo,
    FreeMessageString,
    FreeOperation,
    GetAttribute,
    GetModifier,
    GetPCINumber,GetPCInumber,
    GetPCIVariable,GetPCIvariable,
    Include,
    InitCommand,
    InitDigInfo,
    InitMessageString,
    InitOperation,
    IsToolSame,
    IsVarDefined,
    Len,
    LoadTool,
    MessageBox,
    MessageListBox,
    Mid,
    Option,
    PadText,
    Query,
    QueryDigInfo,
    ReInitCommand,
    Response,
    SaveWithPart,
    SetAttribute,
    SetCallback,SetCallBack,
    SetFeatureVars,
    SetModifier,
    SetModifierEx,
    SetPackage,
    SetPCIVariable,
    ToolbarMods,
    Undo,
    Wait,
},emphstyle={[2]\color{red}}}

\lstdefinestyle{numbers}{numbers=left, stepnumber=1, numberstyle=\tiny, numbersep=10pt}
\lstdefinestyle{nonumbers}{numbers=none}

\lstnewenvironment{sflisting}{\lstset{basicstyle=\sffamily}}{}


%http://stackoverflow.com/questions/3165221/using-listings-how-do-i-maintain-grouping-of-listing-numbers
\lstnewenvironment{JavaScript}[1][]
{
    \renewcommand*{\lstlistingname}{Code (JavaScript)}
    \lstset{#1,language=Java}
}
{
}

\newcommand\JSinput[2][]{%
 \bgroup%
   \renewcommand*{\lstlistingname}{Code (JavaScript)}   
   \lstinputlisting[language=Java,#1]{#2} 
\egroup}


%-------------Create hyperlinks for bookmarks in PDF-------------%
\usepackage{hyperref}


\begin{document}
    \section{this one works}
        \JSinput[caption=\href{https://github.com/AlbanT/JS_PCI/blob/master/afronden2.js}{no spaces in path}, label=code:Afronden, style=numbers]{E:/TEMP/TRUETEMP/truetemp.js}


    \section{unfortunately this one fails if \\usepackage[dutch]\{babel\} is used}
        \JSinput[caption=\href{https://github.com/AlbanT/JS_PCI/blob/master/afronden2.js}{with spaces in path}, label=code:Afronden, style=numbers]{"E:/Cloud storage/GitHub/JS_PCI/afronden2.js"}

\end{document}

查看结果:

在此处输入图片描述

答案1

带有空格或其他特殊字符的文件名需要用引号引起来"..."。但是,当babel加载dutch选项时,则"可以使用简写。使用"引号引起文件名时,需要暂时关闭此功能:

示例输出

\documentclass{article}

\usepackage[main=dutch]{babel}
\usepackage{listings}

\begin{filecontents}{one}
1
\end{filecontents}
\shorthandoff{"}
\begin{filecontents}{"t wo"}
2
\end{filecontents}
\shorthandon{"}

\begin{document}

\lstinputlisting{one}
\shorthandoff{"}\lstinputlisting{"t wo"}\shorthandon{"}

\end{document}

对于任何其他引起问题的速记字符也同样如此。

相关内容