0

I want a command \shybar which prints a \textbar except if it would be the first character on the page. In this case, it should do nothing. Because of this question, I tried the following code:

\documentclass{memoir}

\newcommand*\shybar{\leaders{\mbox\textbar}}

\begin{document}
\shybar This bar should not be printed, but \shybar this one should.
\end{document}

I inserted an \mbox because \leaders asked for a box. Anyway, it did not work; it still asks for boxes. How can I solve this problem?

Gaussler
  • 12,801
  • 1
    There is no concept for the first character on a page. As far as typesetting paragraphs is concerned, you should imagine them printed on a a long scroll of paper, so there's no way (at least with standard TeX) to have \shybar to disappear if it is inside a paragraph and it happens to be the first item on the first line on a new page: paragraphs are typeset before page break decision are made. – egreg Jul 24 '14 at 12:59
  • Well, too bad then... :-) – Gaussler Jul 24 '14 at 13:04
  • Maybe something can be done with LuaTeX. Can you tell why you'd like to do this? – egreg Jul 24 '14 at 13:08
  • you CAN tell if a character is the first in a paragraph using \ifvmode. – John Kormylo Jul 24 '14 at 16:07
  • @egreg I'm converting an XML document (of a classical piece of literature) to LaTeX. \shybar is used to mark a page skip in the original publication. What I want to do is to write \shybar\marginpar{227} to mark page 227. – Gaussler Jul 24 '14 at 16:33
  • @Gaussler I don't understand why you want it to disappear, then – egreg Jul 24 '14 at 16:34
  • @JohnKormylo I looked into \ifvmode, but I don't see exactly how it can be used. Can you show me? :-) – Gaussler Jul 24 '14 at 16:34
  • @egreg Because it also appears in headers and at similar places where it does not look pretty. – Gaussler Jul 24 '14 at 16:35
  • 1
    I tried to use atbegshi and tikzpagenodes to delay drawing the \textbars until \AtBeginShipout, but the bars got drawn on the next page. Interestingly, they showed up in the right spots. – John Kormylo Jul 24 '14 at 22:56

1 Answers1

1

It occurred to me that \tikz[remember page,overlay] must already wait until the page is laid out in order to work right. Nor will it change the layout since it overlays the existing text.

\documentclass{memoir}
\usepackage{tikzpagenodes}

\newlength{\tempx} \newlength{\tempy}

\newcommand\shybar{\ifvmode\tikz[remember picture,overlay]% start of paragraph {\pgfextracty{\tempy}{\pgfpointdiff{\pgfpointorigin}{\pgfpointanchor{current page text area}{north west}}}% \ifdim\tempy<\baselineskip\relax% \else\pgftext[base]{\textbar};\fi}% \else\tikz[remember picture,overlay]% inside paragraph {\let\p1=\pgfpointdiff{\pgfpointorigin}{\pgfpointanchor{current page text area}{north west}}% \pgfextractx{\tempx}{\p1}% \pgfextracty{\tempy}{\p1}% \ifdim\tempy<\baselineskip \ifdim\tempx=0pt \relax% \else\pgftext[base]{\textbar};\fi% \else\pgftext[base]{\textbar};\fi}\fi% \hspace{1pt}}% reserve space

\begin{document} \shybar This bar should not be printed, but \shybar this one should.

\rule{1pt}{7.1in}

Last line of page\newline \shybar This bar should not be printed, but \shybar this one should. \end{document}

This approach has a problem with things like hanging indentation.

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85
John Kormylo
  • 79,712
  • 3
  • 50
  • 120