带有 URL 超链接的 Bookdown 图标题

Bookdown figure captions with URL hyperlink

我一直在尝试使用文本引用在簿记文档中包含图形标题 - 请参见下面的代码:

(ref:cap) See this [link](http://pkgs.rstudio.com/)

```{r fig.cap = '(ref:cap)'}  
plot(mtcars)
```

当我使用 gitbook 格式而不是 pdf 进行编译时,这有效。这是日志显示的内容:

(pdftex.def)             Requested size: 469.75386pt x 325.2142pt.
! Argument of \@tempf has an extra }.
<inserted text> 
                \par 
l.283 ...is \href{http://pkgs.rstudio.com/}{link}}

而且,这是 tex 文件的样子:

\begin{figure}
\centering
\includegraphics{0001-temp_files/figure-latex/unnamed-chunk-1-1.pdf}
\caption{\label{fig:unnamed-chunk-1}See this \href{http://pkgs.rstudio.com/}{link}}
\end{figure}

我可以创建没有超链接的书,但如果可能的话,我真的很想找到一种方法来包含它们。任何帮助将不胜感激!

根据要求,这是完整的 tex 文件(它很丑,因为我是一个初学者!抱歉!):

\PassOptionsToPackage{unicode=true}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames*,x11names*}{xcolor}
%
\documentclass[
  openany]{krantz}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={My book},
  pdfauthor={no name},
  colorlinks=true,
  linkcolor=Maroon,
  filecolor=Maroon,
  citecolor=Blue,
  urlcolor=Blue,
}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[left=2.5cm, right=2.5cm, top=2.5cm, bottom=3.5cm]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.33,0.33,0.33}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.61,0.61,0.61}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.06,0.06,0.06}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.5,0.5,0.5}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0,0,0}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.27,0.27,0.27}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.27,0.27,0.27}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.06,0.06,0.06}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.14,0.14,0.14}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.06,0.06,0.06}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0,0,0}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.27,0.27,0.27}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.43,0.43,0.43}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0,0,0}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.5,0.5,0.5}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.5,0.5,0.5}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0,0,0}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.5,0.5,0.5}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs}
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
  \let\oldparagraph\paragraph
  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
  \let\oldsubparagraph\subparagraph
  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother

\usepackage[square,numbers,sectionbib]{natbib}
\usepackage{booktabs}
\usepackage{amsthm}
 \usepackage{amssymb}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage[titles]{tocloft}
\makeatletter
\def\thm@space@setup{%
  \thm@preskip=8pt plus 2pt minus 4pt
  \thm@postskip=\thm@preskip
}
\usepackage[square,numbers,sectionbib]{natbib}
\usepackage{pdfpages}
\newcommand{\ci}{\perp\!\!\!\perp}
\newcommand{\nci}{\not\!\perp\!\!\!\perp}
\usepackage{tabu}
\makeatother
\let\oldmaketitle\maketitle
\AtBeginDocument{\let\maketitle\relax}
\usepackage{float}
\usepackage{siunitx} 

\usepackage{array}
\newcommand\textstyleStrongEmphasis[1]{\textbf{#1}}
\newcommand\arraybslash{\let\\@arraycr}
\usepackage[utf8]{inputenc}

%\usepackage{uni-titlepage}
\usepackage{titlepic}
\usepackage{graphicx}
\usepackage{framed,color}
\definecolor{shadecolor}{RGB}{248,248,248}

\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\floatpagefraction}{0.75}

\renewenvironment{quote}{\begin{VF}}{\end{VF}}
\let\oldhref\href
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}

%\lstset{
%  breaklines=true
%}  
\usepackage{array}
%\newcommand\textstyleStrongEmphasis[1]{\textbf{#1}}
%\newcommand\arraybslash{\let\\@arraycr}

\usepackage[utf8]{inputenc}
\usepackage{makeidx} 
\makeindex

\let\oldmaketitle\maketitle
\AtBeginDocument{\let\maketitle\relax}

\urlstyle{tt}

\usepackage{amsthm}
\makeatletter
\def\thm@space@setup{%
  \thm@preskip=8pt plus 2pt minus 4pt
  \thm@postskip=\thm@preskip
}
\makeatother

\title{My book}
\author{no name}
\date{2022-05-30}

\begin{document}
\maketitle

%\thispagestyle{empty}
%\newpage

%\maketitle
%\newgeometry{margin=1in}
%\vspace{-1in}
%\begin{center}
%begin{fullsizetitle}
%\hspace*{0.4in}\vspace*{-0.4in}\includegraphics[scale=1]{cover2.pdf}
% The one below almost works
%\hspace*{0.2in}\vspace*{-0.4in}\includegraphics[width=\textwidth]{cover2.pdf}

% This one almost works too
%\includepdf[]{cover2.pdf}
%\end{fullsizetitle}
%\end{center}
%\vspace*{-2cm}\hspace*{-1in}
%\makebox[\paperwidth]{
%        \includegraphics[width=1.3\paperwidth]{cover2.pdf}
%    }
%\restoregeometry

%\newpage
\thispagestyle{empty}

\hspace{0pt}
\vfill
%\newpage
%\thispagestyle{empty}
%\begin{center}
%\includegraphics{_book/frontpage.png}
%\end{center}
\let\maketitle\oldmaketitle
%\maketitle 
%\let\maketitle\oldmaketitle
%\maketitle
\setlength{\abovedisplayskip}{-5pt}
\setlength{\abovedisplayshortskip}{-5pt}
 

\frontmatter
%\pagenumbering{roman}

{
\hypersetup{linkcolor=}
\setcounter{tocdepth}{2}
\tableofcontents
}


\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{0001-temp_files/figure-latex/unnamed-chunk-1-1.pdf}
\caption{\label{fig:unnamed-chunk-1}See this \href{http://pkgs.rstudio.com/}{link}}
\end{figure}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{0001-temp_files/figure-latex/unnamed-chunk-2-1.pdf}
\caption{\label{fig:unnamed-chunk-2}See this site \url{http://pkgs.rstudio.com/}}
\end{figure}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{0001-temp_files/figure-latex/unnamed-chunk-3-1.pdf}
\caption{\label{fig:unnamed-chunk-3}link}
\end{figure}

\backmatter
%\printindex

\end{document}

线路问题

\renewcommand{\href}[2]{#2\footnote{\url{#1}}}

在宏的参数中使用像链接这样可能包含大量特殊字符的脆弱内容不是一个好主意,只是不要这样做。