[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 16:37:59 CEST 2011


On Tue, Oct 11, 2011 at 10:01:42PM +1100, Vafa Khalighi wrote:

> Here is the minimal example (without loading color or xecolor, without
> fontspec, without bidi)

The marking is done by *1*.../1/ for text in one line and
*x*.../x/ for text broken across lines and *?*.../?/ for unknown
state.

\TeXXeTstate=1
\documentclass{article}
\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
}
\newcommand*{\mytextbf at aux}{%
  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 savepos
  \kern1sp\relax
  \zref at savepos
  \zref at labelbyprops{\mytextbf at aux}{posx}%
  \zref at savepos
  \kern-1sp\relax
  \zref at refused{\mytextbf at beg}%
  \zref at refused{\mytextbf at end}%
  \zref at refused{\mytextbf at aux}%
  \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 aux}>%
            \zposx{\mytextbf at beg} %
        \ifnum\zposx{\mytextbf at end}<%
              \zposx{\mytextbf at beg} %
        \else
          \mytextbf at onelinetrue
        \fi
      \else
        \ifnum\zposx{\mytextbf at end}>%
              \zposx{\mytextbf at beg} %
        \else
          \mytextbf at onelinetrue
        \fi
      \fi
    \fi
  \fi
  \nobreak\hspace\z@
  \zref at ifrefundefined{\mytextbf at beg}{%
    \MarkLines{?}{\textbf{#1}}%
  }{%
    \ifmytextbf at oneline
      \MarkLines{1}{\textbf{#1}}%
    \else
      \MarkLines{x}{\textbf{#1}}%
    \fi
  }%
  \zref at savepos
  \zref at labelbyprops{\mytextbf at end}{abspage,posx,posy}%
  \zref at savepos
}

\newcommand*{\MarkLines}[2]{%
  \begingroup
    \sbox0{\begin{tabular}{@{}l@{}}1\\x\\?\end{tabular}}%
    *%
    \makebox[\wd0]{#1}%
    *%
    #2%
    /%
    \makebox[\wd0]{#1}%
    /%
  \endgroup
}
\begin{document}
\beginR
foo \mytextbf{include enabled primitives} bar bla bla bla
\mytextbf{include enabled primitives} bla
\mytextbf{include enabled primitives} end

\end{document}


Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list