0

There are several page styles in a document, and I want to change the appearance of some paragraphs if the page style they appear on is, say, plain, with a macro of roughly the following form:

\def\plainpagestyle{plain}
\def\someparagraph#1{
    \ifx???\currentpagestyle???\plainpagestyle
        \textbf{#1}
    \else
        \textit{#1}
    \fi
}

so that \someparagraph{some text} renders "some text" in bold on plain pages and in italics elsewhere.

More specifically, I want the appearance of some paragraphs to change on pages a part or a chapter or other things like this begin, so always after \thispagestyle{plain} is declared on the page.

Is there a nice way to do it?

  • by default this is not recorded anywhere – David Carlisle Dec 19 '23 at 20:45
  • apart from the fact that \pagestyle does not record its argument, it is not known which page a text will land on at the point that fonts are used to typeset the text. – David Carlisle Dec 19 '23 at 20:47
  • Yes, I understand that it might not be possible in general case. I only need this condition to be true if \thispagestyle was declared to be plain on the same page the paragraph initially starts, and if it ends up on the next page, it's ok, pretty much like \thepage doesn't update until a new paragraph starts. – Sergey Slyusarev Dec 19 '23 at 21:07
  • What do you mean by 'the same page the paragraph initially starts'? Initially, the paragraph doesn't start on any page at all. – cfr Dec 19 '23 at 21:10
  • 1
    there is no "thispagestylethe\thispagestylecommand takes an argument like\pagestyleand just works for one page, If\thispagestylewas used the argument is recorded in@specialstylebut if\pagestyle` was used the argument is not recorded at all. – David Carlisle Dec 19 '23 at 21:13
  • What are you actually trying to do? Can you give a proper example? You don't normally have any paragraphs (or \paragraphs) on pages with \part so are you actually just trying to change something on the first page (and maybe a bit later) of chapters? – cfr Dec 19 '23 at 21:29
  • @cfr There's a book. One quirk of this particular book is that section titles don't actually appear on the page, they only appear in page's header, unless there's no header on the page (i.e. pagestyle is plain). In such a case section titles do appear. The latter happens in the beginning of the book's parts (or chapters), which, in case of this particular book, do contain text and normally have sections starting right below the part/chapter titles. In the past I solved this problem by simply making two versions of section macros, but I want to make it automatic and more flexible. – Sergey Slyusarev Dec 19 '23 at 21:51
  • @David Carlisle Yes, I understand how \thispagestyle and \pagestyle work, and tried poking \@specialstyle, to no avail. My only idea right now is to record \thepage at the end of the \part definition and compare it to \thepage at the beginning of the paragraph in question, but maybe there's a better way. – Sergey Slyusarev Dec 19 '23 at 21:58
  • \@specialpagestyle will be defined to be plain but you really shouldn't be testing for that. – David Carlisle Dec 19 '23 at 22:02
  • That means you are already using non-standard code for \part, at least. Yet you've not provided any example which might hint at what you're doing. If you use customised pages, you could include a call to LaTeX's hooks there and use that mechanism to switch the definitions. With fancyhdr, for example, you could change the definition in, say, fancyplain and include a hook there to revert the change when the page is shipped out. Then ensure \chapter, \part etc. use fancyplain rather than plain. (I'm assuming you want regular plain somewhere.) – cfr Dec 20 '23 at 03:21

2 Answers2

1

(Too long for a comment)

This is not going to work, unless your \someparagraph is guaranteed to appear within the scope of \thispagestyle{plain}, no matter what.

Typically, TeX is typesetting paragraphs before having decided where to split pages and so it may happen that when it encounters your \someparagraph it still believes that \thispagestyle{plain} is in force, but the text will eventually appear past the page break.

Not to mention that the text itself might be split across pages and, in this case, it will be wrong no matter what you do. OK, we could try with something based on \label and \ref, the asynchronous mechanism that makes possible to have correct page references.

Now the fun begins: the size of the text will be different if you typeset it in boldface or in italics; boldface might trigger a page break and you'll end up with the text in a page with normal style, but in boldface. So, even an asynchronous mechanism might fail, because at the next typesetting you'd get italics, which might not trigger the page break. Can you see the risk of an infinite loop?

You have to better delimit your problem, if you hope to get an answer.

egreg
  • 1,121,712
  • "No" is an answer too. I totally realize that it's not going to work reliably, and to solve my specific problem I might just try something completely different. However, I tried to find the way to specifically get current page style (like you can sort of get current page number with \thepage, albeit with all the caveats you mentioned) and didn't find it, so I figured, for the next person to try and do the same thing it makes sense to make it a question here so that they can at least see that it's not something you can do easily. – Sergey Slyusarev Dec 20 '23 at 20:04
0

If your test is

has \thispagestyle been issued AND the page style selected is plain

then the following will work

\makeatletter
\def\@SS@plain{plain}
\newcommand\whatisthepagestyle{%
        \if@specialpage
                \ifx\@specialstyle\@SS@plain
                        PAGE IS PLAIN
                \else
                        PAGE IS SPECIAL, NOT PLAIN
                \fi
        \else
                PAGE IS NOT SPECIAL
        \fi
}
\makeatother
  • This will not test whether the current page style is plain, but only whether on the current page a call to \thispagestyle{plain} has been issued. If someone calls \pagestyle{plain} on the page followed by \pagestyle{empty} on the following page, no can do.
  • This will behave funny if \thispagestyle{plain} and \thispagestyle{something-else} appears on the same page with some intervening text that uses the test.
  • This will behave funny if \thispagestyle{plain} is set relatively late on the page.
  • This assumes that nothing messed with the definition of \thispagestyle.
  • This will almost certainly bleed a little into the next page; but from your question and your comments it appears you are okay with that.

Is your question really about testing for \thispagestyle{plain}? Or is it secretly an X-Y problem where the real issue is to change certain styles on the pages where \chapter and \part are issued? If it is the latter, a better (IMHO) way is to

\AddToHook{shipout/after}{\gdef\someparagraph\textit}

and to hook into your styling of \chapter and \part to define \gdef\someparagraph\textbf when they are called. This can be done either using titlesec (which allows you to specify some code to be run after section styling is applied), or use etoolbox and something like \pretocmd\{\chapter}{\gdef...}.

Willie Wong
  • 24,733
  • 8
  • 74
  • 106