LaTeX Socket Error: Cannot knit Rmd Document
Asked Answered
S

3

5

I'm currently working on my thesis and have been using a bookdown template provided by one of the academics in the department but I've run into an issue knitting it to a pdf.

Whenever I try to do so it fails and I get the following error: ! LaTeX socket Error: Sockets can only be declared at top-level!

I have been able to knit the document many times in the past and only came across this issue recently. I was initially able to fix it by removing the tinytex package and reinstalling it, but this is no longer working and I cannot get the file to knit. The .tex file is created just fine, its in the compiling of the tex file to a pdf that causes the issue. When I attempt to compile the tex file on its own the error is said to be related to the hyperref.sty file used in the compiling, but I genuinely have no idea why or how to fix this.

I have also already tried updating both my R and R studio, and deleting and re-installing LaTeX (LaTeXIT) on my laptop (Macbook Pro if that helps diagnose this issue), but nothing is making any difference. Neither has clearing the cache, nor re-downloading the template and knitting a blank template. I first encountered this issue when I tried to use split to align my equations, and have had issues since.

Here is the bottom section of the log from when I try to knit if that is of any help:

Package: intcalc 2019/12/15 v1.3 Expandable calculations with integers (HO)
)
\Hy@SavedSpaceFactor=\count411

! LaTeX socket Error: Sockets can only be declared at top-level!

For immediate help type H <return>.
 ...                                              
                                                  
l.2080  {\providecommand\UseSocket[2]{#2}}
                                           
Here is how much of TeX's memory you used:
 15152 strings out of 476232
 276012 string characters out of 5795949
 1924530 words of memory out of 5000000
 36931 multiletter control sequences out of 15000+600000
 562584 words of font info for 39 fonts, out of 8000000 for 9000
 14 hyphenation exceptions out of 8191
 81i,0n,95p,596b,9378s stack positions out of 10000i,1000n,20000p,200000b,200000s

!  ==> Fatal error occurred, no output PDF file produced!

For further background, when this issue happened most recently I removed all of the newly added content from the document and tried to knit it and it still wouldn't knit, even after repeating all the steps I listed above. If anybody could please help that would be greatly appreciated!

Skyjack answered 11/11, 2023 at 13:39 Comment(2)
Have you tried (re-)installing the intcalc macro? Or tried using Overleaf to generate the pdf from the .tex file?Necessarian
I didn't hav the intcalc macro installed to begin with, but installing it doesn't make any difference. Overleaf likely won't work as I have R code embedded within the markdown files for graphs and tables, etc.,Skyjack
P
8

As mentioned in the hyperref bug report https://github.com/latex3/hyperref/issues/315, \spacing{1} is wrong input. spacing is an environment, not a command and \spacing{1} opens a group and this breaks hyperref.

Replace \spacing{1} by \singlespacing or \setstretch{1}. Or simply remove it.

Patricio answered 20/11, 2023 at 20:21 Comment(2)
This appears to break the spacing package then.Maryleemarylin
@Maryleemarylin there is no spacing package, so I don't know what you mean.Patricio
C
3

I had the same LaTeX socket error suddenly pop up two days ago. Since I'm also new to latex and R Markdown I relied on a template that included these packages:

header-includes:
- \usepackage[ngerman]{babel}
- \usepackage{float}
- \usepackage{sectsty}
- \usepackage{paralist}
- \usepackage{setspace}\spacing{1}
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \usepackage{dcolumn}
- \usepackage{csquotes}
- \usepackage[nottoc, numbib]{tocbibind}

This worked fine until that day. Since compiling a simple bookdown document with biblatex (without those packages) still worked fine, I removed package by package until no error resulted. Turns out, I just had to remove \spacing{1}.

Cashman answered 17/11, 2023 at 17:41 Comment(2)
Thank you! I can confirm that removing \spacing{1} can fix the error, although I don't know why.Prudhoe
For the record, I have filed an issue to github.com/latex3/hyperref/issues/315.Prudhoe
F
-1

In my case it was missing the line \usepackage[unicode]{hyperref} % backref linktocpage pagebackref in my main document. Adding the line solved everything for me.

Fucoid answered 3/2, 2024 at 10:44 Comment(1)
The hyperref manual has the following to say about the unicode option: "starting with version v7.00g set by default to true for all engines". If your tex distribution is up-to-date, you shouldn't need this option. Also rmarkdown normally loads hyperref automatically.Ruth

© 2022 - 2025 — McMap. All rights reserved.