ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: mf <massifr@fastwebnet.it>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: changing alignment inside a paragraph
Date: Wed, 11 Dec 2019 15:31:21 +0100	[thread overview]
Message-ID: <3586f8c3-5aa4-4918-6c98-fee59b33ad8e@fastwebnet.it> (raw)
In-Reply-To: <ebd94815-1a93-c9d5-28db-78881f395157@fastwebnet.it>

This is a good-enough solution to my problem:

\setupalign[stretch]

\starttext

\def\Text{This is the text of an index term, that is normally justified,
but I'd like the page numbers to be flushed to the right from
their second line}

\def\Pages{34, 57, 101-104, 276, 345, 401, 403, 512.}

\type{\PlaceIndexTerm} has 6 parameters:
\startitemize[n]
\item the main text
\item the pages' numbers
\item the text width
\item the left margin of the lines made only of page numbers
\item the width of the separator (an hbox) before the first page number, 
when there's room
       in the last line of the text
\item the minimum accepted space left at the end of the last line of 
text, to put a page number
\stopitemize

\def\PlaceIndexTerm#1#2#3#4#5#6{%
\newdimen\TextWidth \TextWidth=#3
\newdimen\SBPN \SBPN=#5 % space before page numbers
\newdimen\PagesIndentation \PagesIndentation=#4
\newdimen\MSLLL \MSLLL=#6 % minimum space left on last line
\parshape 1 0pt \TextWidth #1\par
\newdimen\LLW \LLW=\lastlinewidth
\page[no]
\ifdim \dimexpr\TextWidth-\LLW-\SBPN > \MSLLL 
\blank[-\the\lineheight,fixed]\fi
\startalignment[flushright,stretch]
\ifdim \dimexpr\TextWidth-\LLW-\SBPN > \MSLLL
\parshape 2
\LLW \dimexpr \TextWidth - \LLW
\PagesIndentation \dimexpr \TextWidth - \PagesIndentation \red
\else
\parshape 1 \PagesIndentation \dimexpr \TextWidth - \PagesIndentation 
\darkgreen
\fi
\noindent\ifdim \dimexpr\TextWidth-\LLW-\SBPN > \MSLLL\hbox to 
\SBPN{}\fi #2\par
\stopalignment}

\def\PlaceIndexTermDescribed#1#2#3#4#5#6{%
\newdimen\TW\TW=#3
\noindent{\tfx width={\bf \PtToCm{\the\TW}},
pages' left margin (2nd line)={\bf #4},
hbox before pages={\bf #5},\\%
min space left on last line of text={\bf #6}}\par
\blank[halfline]
\PlaceIndexTerm{#1}{#2}{#3}{#4}{#5}{#6}
\blank}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{3cm}{12pt}{1em}{12pt}
In this case there's no space left on the last line of text,
so the page numbers are flushed to the right, starting from
a new line.

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{3.5cm}{12pt}{1em}{12pt}
Now the last line of text is short and there's room to start the page
numbers on the same line. The remainder of page numbers is flushed right,
with a (min) left margin of 12pt.\par
Actually, the page numbers are started in a new paragraph with a 
\type{\parshape}
that depends on the last line of the text; then the new paragraph is moved
upwards with a \type{\blank[-\the\lineheight,fixed]}.
A \type{\page[no]} before the \type{\blank} guarantees that
no page break occurs between text and numbers.

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{4cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{4.5cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{5cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{5.5cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{6cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{6.5cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{7cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{7.5cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{8cm}{12pt}{1em}{12pt}
That \type{512} left on the last line is ugly, and we can trim the 
separator hbox
from \type{1em} to \type{.1em} to let all the page numbers fit in the 
last line:\par
\blank
\PlaceIndexTermDescribed{\Text}{\Pages}{8cm}{12pt}{.1em}{12pt}
or we could increase the left margin of the lines made only of page numbers
(to \type{3cm} in this case), to distribute the numbers better across 
the two lines:\par
\blank
\PlaceIndexTermDescribed{\Text}{\Pages}{8cm}{3cm}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{8.5cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{9cm}{12pt}{1em}{12pt}

\page
\PlaceIndexTermDescribed{\Text}{\Pages}{9.5cm}{12pt}{1em}{12pt}
Increasing the left margin of the lines of page numbers gives a better 
result:\par
\blank
\PlaceIndexTermDescribed{\Text}{\Pages}{9.5cm}{4cm}{1em}{12pt}

\page
Full text width (third parameter set to \type{\textwidth}):\par
\blank
\PlaceIndexTermDescribed{\Text}{\Pages}{\textwidth}{12pt}{1em}{12pt}

\stoptext

(Thanks again to Wolfgang for answering my question)

Massi
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  parent reply	other threads:[~2019-12-11 14:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-10 11:27 mf
2019-12-10 17:07 ` Wolfgang Schuster
2019-12-11 14:31 ` mf [this message]
2019-12-11 20:25   ` Wolfgang Schuster
2019-12-12 14:56     ` mf
2019-12-12 20:43       ` Wolfgang Schuster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3586f8c3-5aa4-4918-6c98-fee59b33ad8e@fastwebnet.it \
    --to=massifr@fastwebnet.it \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).