Bookdown中含URL的图例在pdf_book输出异常问题求助
I’ve run into this exact issue before—markdown links in figure captions work seamlessly for HTML outputs like gitbook, but LaTeX (which powers bookdown::pdf_book) doesn’t parse them natively. Here’s how to get your URLs rendering correctly in PDFs:
Why This Happens
When generating a PDF with bookdown::pdf_book, your document is compiled to LaTeX first. Markdown-style links ([text](url)) aren’t processed correctly in LaTeX captions, leading to broken formatting, missing links, or even compilation errors.
Solution 1: Use LaTeX’s \url{} Command
The simplest fix is to replace markdown links with LaTeX’s built-in \url{} command. Bookdown includes the hyperref package by default, so this will create clickable, properly formatted URLs in your PDF.
Here’s how to adjust your example code:
--- output: bookdown::pdf_book --- ## Reference with URL works now In this experiment, we will use a spectrophotometer to measure the absorption of light of different wavelengths by colored solutions. (ref:spectrum) Spectrum of light. V, violet; B, blue; G, green; Y, yellow; O, orange; R, red. For more details on spectrophotometry, see \url{https://en.wikipedia.org/wiki/Spectrophotometry} 
Solution 2: Custom Link Text with \href{}
If you want to display custom text instead of the full URL, use LaTeX’s \href{url}{display-text} command. This keeps your caption clean while still providing a clickable link:
(ref:spectrum) Spectrum of light. V, violet; B, blue; G, green; Y, yellow; O, orange; R, red. Learn more about \href{https://en.wikipedia.org/wiki/Spectrophotometry}{spectrophotometry} here.
Quick Tips
- Avoid spaces between
\url{and the URL, or between the URL and}—LaTeX is strict about syntax here. - Special characters in URLs (like underscores) are automatically handled by
\url{}, so no need to escape them manually.
内容的提问来源于stack exchange,提问作者Nikolaus

