knitr 在深层超链接中创建错误的第二个“#”

knitr 在深层超链接中创建错误的第二个“#”

我想.Rnw在 RStudio 中编译一个文档,knitr其中浮动标题的超链接(深层链接)包含“#”。

参见最小工作示例: (使用 R Studio 版本 0.99.435、pdfTeX 版本 3.14159265-2.6-1.40.15(TeX Live 2015/dev/Debian)编译)

\documentclass{article}
\usepackage{hyperref}
\begin{document}

<<TestPlot, fig.cap='\\href{http://amor.cms.hu-berlin.de/~schulzgu/gentri/#mieteaenderung}{Click here and u will see the link contains \\#\\# instead of \\#}', echo=FALSE>>=
plot(rnorm(30))
@
In contrast, a plain Latex Link containing a \# in the URL works:
\href{http://amor.cms.hu-berlin.de/~schulzgu/gentri/#mieteaenderung}{Click here}
\end{document}

PDF 可以编译,但标题中的超链接现在包含“#” 而不是 1 - 因此会将读者引导到错误的 URL。

看起来 knitr 插入了第二个 #,但实际上应该只有一个。

有没有什么解决方法或者合适的解决方案?


.log文件给出了以下错误:

! Illegal parameter number in definition of \GetTitleStringResult.
<to be read again> 
               m
l.58 ...see the link contains \#\# instead of \#}}
                                              \label{fig:TestPlot}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! Illegal parameter number in definition of \GTS@GlobalString.
<to be read again> 
               m
l.58 ...see the link contains \#\# instead of \#}}
                                              \label{fig:TestPlot}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

 ! Illegal parameter number in definition of \reserved@a.
<to be read again> 
               m
l.58 ...see the link contains \#\# instead of \#}}
                                              \label{fig:TestPlot}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! Illegal parameter number in definition of \Hy@tempa.
<to be read again> 
               m
l.58 ...see the link contains \#\# instead of \#}}
                                              \label{fig:TestPlot}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! Illegal parameter number in definition of \Hy@tempa.
<to be read again> 
               m
l.58 ...see the link contains \#\# instead of \#}}
                                              \label{fig:TestPlot}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! Illegal parameter number in definition of \Hy@tempa.
    <to be read again> 
               m
l.58 ...see the link contains \#\# instead of \#}}
                                              \label{fig:TestPlot}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

Package atveryend Info: Empty hook `BeforeClearDocument' on input line 67.
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}

答案1

标题中定义的链接中的字符 # 需要使用双反斜杠进行转义。

尝试这个:

\documentclass{article}
\usepackage{hyperref}
\begin{document}

<<TestPlot, fig.cap="\\href{http://amor.cms.hu-berlin.de/~schulzgu/gentri/\\#mieteaenderung}{If you escape the character \\# in the link with a double backslash, then everything is fine.}", echo=FALSE>>=
plot(rnorm(30))
@

This is a special situation connected with the chunk elaborated by R. You don't need to add the escape characters in plain \LaTeX in the case of a link to a URL containing a \# :
\href{http://amor.cms.hu-berlin.de/~schulzgu/gentri/#mieteaenderung}{Click here}

\end{document}

在此处输入图片描述

相关内容