Skip to content

replace \AtBeginDocument and \AtEndDocument in extra LaTeX code used in documents to be previewed#4322

Merged
sunderme merged 1 commit intotexstudio-org:masterfrom
octaeder:previewArticle
Feb 7, 2026
Merged

replace \AtBeginDocument and \AtEndDocument in extra LaTeX code used in documents to be previewed#4322
sunderme merged 1 commit intotexstudio-org:masterfrom
octaeder:previewArticle

Conversation

@octaeder
Copy link
Contributor

@octaeder octaeder commented Feb 5, 2026

This PR resolves #4320. The problem described arises because the document class being used and TeXstudio’s PDF-preview both attempt to use the same LaTeX mechanism to achieve certain modifications, and in doing so they interfere with each other. They use the commands \AtBeginDocument and \AtEndDocument to allow code to be specified in the document preamble that should be inserted at the beginning or end of the text written or selected for preview. The issue is resolved by having TeXstudio place the required modifications directly at the start and end of a temporary text used for preview, in exactly the same way these commands would.

Description of the changes (line numbers from changed files):
buildmanager.cpp builds up the document env. But here it's not clear if any changes should be added. This is well known in texstudio.cpp (l. 9432 and 9474). Thus we build necessary changes there and pass them as new parameters to buildmanager.cpp (s. buildmanager.h). The code in l. 9474 in fact was the final chapter of Texstudio::makePreviewHeader which is moved to Texstudio::showPreview.

Special behavior for Beamer class is not changed in any way.

Note: The discussion in comments below may appear somewhat outdated, as the description above has changed quite a lot (including PR's title).

Example of LaTeX document
\documentclass[aip,apl,amsmath,amssymb,reprint,linenumbers]{revtex4-2}
\begin{document}
Several mechanisms can lead to significant Doppler broadening of hydrogen Balmer
\end{document}

Assume that the whole text line has been selected for preview.

Code finally compiled for **inline** preview
\documentclass[aip,apl,amsmath,amssymb,reprint,linenumbers]{revtex4-2}
\usepackage[active,tightpage]{preview}
\usepackage{varwidth}
\pagestyle{empty}
\begin{document}
\begin{preview}\begin{varwidth}{\linewidth}
Several mechanisms can lead to significant Doppler broadening of hydrogen Balmer
\end{varwidth}\end{preview}
\end{document}
Code finally compiled for preview in embedded PDF-viewer
\documentclass[aip,apl,amsmath,amssymb,reprint,linenumbers]{revtex4-2}
\pagestyle{empty}
\begin{document}
Several mechanisms can lead to significant Doppler broadening of hydrogen Balmer
\end{document}

@sunderme
Copy link
Member

sunderme commented Feb 5, 2026

I think, this needs to be discussed in more details first.
The current solution replaces only beamer and nothing else. It is on by default.

This can not happen with a general replacement, especially as classes tend to load relevant packages which are now disabled in preview.

@octaeder
Copy link
Contributor Author

octaeder commented Feb 5, 2026

so it would me better to have a second option for this, which is not on by default (and maybe more things)

@sunderme
Copy link
Member

sunderme commented Feb 5, 2026

Yes, i would leave the beamer part as is.
And then we can think about another option.

However, are there any other cases than revtex which fail and need this ?

@octaeder
Copy link
Contributor Author

octaeder commented Feb 5, 2026

I use only book, article or report. So I have no specific knowledge here and I can't remember any similar issue opened here in the past. Maybe @muzimuzhi has heard about it? Are there known issues or usage restrictions in the documentation of the packages added for preview?

@octaeder octaeder marked this pull request as draft February 5, 2026 19:46
@muzimuzhi
Copy link
Contributor

muzimuzhi commented Feb 5, 2026

there a number of lua related things that can not be dumped in a format, see the answer and the comments here tex.stackexchange.com/questions/324559/…

TeX-SX comment by Ulrike Fischer

So the list of unsupported document classes would be long. Also it's hard to determine if a class supports format dumping, without checking its implementation.

For example, if beamer is unsupported, I suppose its successor ltx-talk is unsupported too.

Related TeX-SX post(s)

If I remember correctly, XeLaTeX also doesn't support dumping OpenType fonts.

@octaeder
Copy link
Contributor Author

octaeder commented Feb 5, 2026

@muzimuzhi Where comes dumping into place? I remember having issues when implementing preview for lualatex. The reason was that option Precompile Preamble (from Preview) was set and dumping (the result of the precompile) didn't work with tikz. But preview worked when the option was not set. Is it this you are talking about?

@octaeder octaeder changed the title preview option to replace any document class by article do not use \AtBeginDocument and \AtEndDocument in additions for preview Feb 7, 2026
@octaeder octaeder changed the title do not use \AtBeginDocument and \AtEndDocument in additions for preview replace \AtBeginDocument and \AtEndDocument in extra LaTeX code used in documents to be previewed Feb 7, 2026
@octaeder octaeder marked this pull request as ready for review February 7, 2026 01:47
@sunderme sunderme merged commit af401a9 into texstudio-org:master Feb 7, 2026
8 checks passed
@sunderme
Copy link
Member

sunderme commented Feb 7, 2026

thanks

@octaeder octaeder deleted the previewArticle branch February 7, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preview selection (Alt+P) compile with error, no display

3 participants