# Vertical bar appearing except on top of page

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}

\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?

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

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.