[texhax] checking if a piece of text stays on one line or more than one line
Heiko Oberdiek
heiko.oberdiek at googlemail.com
Tue Oct 11 09:23:43 CEST 2011
On Tue, Oct 11, 2011 at 03:56:57PM +1100, Vafa Khalighi wrote:
> Can you please send a minimal example showing how this works because all my
> attempts with your package so far failed.
\documentclass{article}
\usepackage{color}
\usepackage{zref-savepos}
\makeatletter
\newcounter{mytextbf at count}
\newcommand*{\mytextbf at label}[1]{%
mytextbf@#1@\the\value{mytextbf at count}%
}
\newif\ifmytextbf at oneline
\newcommand*{\mytextbf}[1]{%
\leavevmode
\stepcounter{mytextbf at count}%
\zsavepos{\mytextbf at label{beg}}%
\mytextbf at onelinefalse
\ifnum\zposy{\mytextbf at label{beg}}=\zposy{\mytextbf at label{end}} %
\ifnum\zposx{\mytextbf at label{end}}<\zposx{\mytextbf at label{beg}} %
\else
\mytextbf at onelinetrue
\fi
\fi
\nobreak\hspace{0pt}% allow hyphenation
\zref at ifrefundefined{\mytextbf at label{beg}}{%
\textbf{#1}%
}{%
\ifmytextbf at oneline
\textcolor{blue}{\textbf{#1}}%
\else
\textcolor{red}{\textbf{#1}}%
\fi
}%
\zsavepos{\mytextbf at label{end}}%
}
\begin{document}
foo \mytextbf{include enabled primitives} bar bla bla bla
\mytextbf{include enabled primitives} bla
\mytextbf{include enabled primitives} end
\end{document}
And an extended version that completes the position by the absolute
page number and uses \newcount to avoid too much trouble with \include.
\documentclass{article}
\usepackage{color}
\usepackage{zref-savepos,zref-abspos}
\makeatletter
\newcount\mytextbf at count
\mytextbf at count=\z@
\newcommand*{\mytextbf at beg}{%
mytextbf-\the\mytextbf at count
}
\newcommand*{\mytextbf at end}{%
mytextbf+\the\mytextbf at count
}
\newif\ifmytextbf at oneline
\newcommand*{\mytextbf}[1]{%
\leavevmode
\global\advance\mytextbf at count\@ne
\zref at savepos
\zref at labelbyprops{\mytextbf at beg}{abspage,posx,posy}%
\zref at refused{\mytextbf at beg}%
\zref at refused{\mytextbf at end}%
\mytextbf at onelinefalse
\ifnum\zref at extract{mytextbf at beg}{abspage}=%
\zref at extract{mytextbf at end}{abspage} %
\ifnum\zposy{\mytextbf at beg}=%
\zposy{\mytextbf at end} %
\ifnum\zposx{\mytextbf at end}<%
\zposx{\mytextbf at beg} %
\else
\mytextbf at onelinetrue
\fi
\fi
\fi
\nobreak\hspace\z@
\zref at ifrefundefined{\mytextbf at beg}{%
\textbf{#1}%
}{%
\ifmytextbf at oneline
\textcolor{blue}{\textbf{#1}}%
\else
\textcolor{red}{\textbf{#1}}%
\fi
}%
\zref at savepos
\zref at labelbyprops{\mytextbf at end}{abspage,posx,posy}%
}
\begin{document}
foo \mytextbf{include enabled primitives} bar bla bla bla
\mytextbf{include enabled primitives} bla
\mytextbf{include enabled primitives} end
\end{document}
Adding support for right-to-left-writing is left as exercise for you.
Yours sincerely
Heiko Oberdiek
More information about the texhax
mailing list