rstudio / tinytex

A lightweight, cross-platform, portable, and easy-to-maintain LaTeX distribution based on TeX Live
https://yihui.org/tinytex/
Other
983 stars 116 forks source link

Encountering `LaTeX Socket Error` upon compiling LaTeX documents #430

Closed szmsu2011 closed 10 months ago

szmsu2011 commented 10 months ago

Today, I reinstalled TinyTeX with tinytex::reinstall_tinytex(). The reinstallation was successful but I immediately ran into issues when trying to render a bookdown document with bookdown::render_book() or directly compile a LaTeX document with tinytex::latexmk(), they both stopped by error

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

tinytex::latexmk() was working immediately before I reinstalled TinyTex, and this was the only change made (the issue persists after updating all softwares, R, RStudio, packages, macOS, etc.).

Upon searching on the Internet I only managed to find only two reports (in fact, by the same person) for the same issue, very recently, on Nov 11, 2023, so it seems like a new-emerging issue:

https://community.rstudio.com/t/latex-socket-error-cannot-knit-rmd-document/177023 https://stackoverflow.com/questions/77465287/latex-socket-error-cannot-knit-rmd-document

I also tried to reinstall a previous version of TinyTex:

uninstall_tinytex()
install_tinytex(version = "v2023.08")

The error persists after trying many versions.

With the information and knowledge I have, I cannot tell if the issue is on this package or the TeX Live distribution.

Session info:

xfun::session_info('tinytex')
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 14.1.1

Locale: en_NZ.UTF-8 / en_NZ.UTF-8 / en_NZ.UTF-8 / C / en_NZ.UTF-8 / en_NZ.UTF-8

Package version:
  graphics_4.3.2  grDevices_4.3.2 stats_4.3.2     tinytex_0.48.4 
  tools_4.3.2     utils_4.3.2     xfun_0.41 

LaTeX version used: 
  TeX Live 2023 (TinyTeX) with tlmgr 2023-11-12

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 10 months ago

The reinstallation was successful but I immediately ran into issues when trying to render a bookdown document with bookdown::render_book() or directly compile a LaTeX document with tinytex::latexmk(), they both stopped by error

Does it happens only with this project ?

Or does it happen with a simple pdf_document() rendering ?

stopped by error

Can you share the whole error please. What is surrounding the error in the log file.

In the SO post you shared we see

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>.

So this could be related to intcalc package maybe.

We haven't add this issue ourselves with the latest version of TeX Live.

Also TinyTeX is tested before being released daily (today's tests in gha) and we have no issue while rendering our tests docs (simple document or demo book).

It seems related to TeX Live itself or maybe this comes from a specific CTAN package used in your project.

As mentioned on community, this errors could come from Socket package: https://ctan.space-pro.be/tex-archive/macros/latex-dev/base/ltsockets-doc.pdf

related to another.

Anyhow, hard to debug for us without being able to reproduce.

You could try simplify your project until it renders to find what could be the cause.

yihui commented 10 months ago

The answer at https://stackoverflow.com/a/77503626/559676 can fix the problem, and I'm still investigating it to understand why (it seems to be caused by a recent update of hyperref).

Here is a minimal reprex:

---
header-includes:
  - \usepackage{setspace}\spacing{1}
output:
  pdf_document:
    keep_tex: true
---

Test.
cderv commented 10 months ago

Thanks @Yihui!

szmsu2011 commented 10 months ago

Yes, it seems like the suggestion at https://stackoverflow.com/a/77503626/559676 did fix the issue for me. Previously I did not bother but after removing/commenting out every single control sequence related to the setspace package from the .cls file in my project fixed the problem.

Before the update I was using a very old version of TinyTeX and the setspace package. Can’t recall the exact versions now but I remembered it was installed in late 2020.

u-fischer commented 10 months ago

As explained in the hyperref issue (https://github.com/latex3/hyperref/issues/315) \spacing{1} is faulty syntax. It opens a group and hyperref is then loaded inside this group. The new socket code does not like this.

Remove the command or replace it with a correct command from setspace.

yihui commented 10 months ago

@u-fischer Thanks! That's very helpful!

@szmsu2011 I wonder how you got the command \spacing{} in your document. For Markdown documents, you can use the variable linestretch in YAML to set the spacing: https://pandoc.org/MANUAL.html#variables-for-latex, which will generate the correct command as @u-fischer mentioned (i.e., \setstretch{}, instead of \spacing{}).

szmsu2011 commented 10 months ago

It was in the .cls file from a bookdown template I am using. Although the file was quite large, only a very small proportion contributed to issue:

\RequirePackage{setspace}
\spacing{1.5}
\AtBeginDocument{%
\ifsinglesp@%
  \singlespacing%
\else%
  \doublespacing%
\fi%
}

The prior is not a good way to control line spacing but I wasn’t aware of them until they became a problem.

yihui commented 10 months ago

@szmsu2011 I see. I think it will be great if you can notify the template author to correct the problem.