Plain TeX: Problèmes lorsque \ loop pour définir les paramètres de \ parshape

Dec 21 2020

J'essayais d'écrire une macro pour envelopper du texte autour d'une figure. Je sais qu'il est disponible en Latex et qu'il existe un autre package, figflow pour plainTex. J'apprends juste à écrire le mien. Ma première étape a été d'utiliser \ parshape pour créer une zone vide pour la figure. Je suis resté coincé ici. Je définis la macro comme suit:

\newcount\ln
\def\wrap#1#2{\parshape=#1 \loop\relax\ifnum\ln<#1 0cm #2 \advance\ln by 1\repeat}

Ça ne marche pas. J'ai essayé \edef, ce qui n'a pas fonctionné non plus. Quelqu'un peut-il aider? Merci!

\parshape{6}{10cm} Here is the definitive guide to the use of TeX, written by the system's creator, Donald E. Knuth. TeX represents the state of the art in computer typesetting. It is particularly valuable where the document, article, or book to be produced contains a lot of mathematics, and where the user is concerned about typographic quality. TeX software offers both writers and publishers the opportunity to produce technical text of all kinds, in an attractive form, with the speed and efficiency of a computer system. Novice and expert users alike will gain from The TeXbook the level of information they seek. Knuth warns newcomers away from the more difficult areas, while he entices experienced users with new challenges. The novice need not learn much about TeX to prepare a simple manuscript with it. But for the preparation of more complex documents, The TeXbook contains all the detail required. Knuth's familiar wit, and illustrations specially drawn by Duane Bibby, add a light touch to an unusually readable software manual. The TeXbook is the first in a five-volume series on Computers and Typesetting, all authored by Knuth
\bye

Réponses

3 Skillmon Dec 21 2020 at 02:29

Une réponse montrant une boucle assez basique avec \numexpr.

Lorsque vous utilisez \parshapeet que vous souhaitez qu'un nombre spécifique de lignes soit affecté, vous devez ajouter une ligne supplémentaire avec 0cm \hsizepour revenir aux lignes normales après avoir \parshapeterminé, car \parshapela dernière spécification sera répétée pour chaque ligne restante du paragraphe.

\catcode`\@=11
\long\def\@gobble#1{}
\long\def\@secondoftwo#1#2{#2}
\def\wrap#1#2%
  {%
    \parshape=\numexpr#1+1\relax \wrap@{0}{#1}{#2} 0cm \hsize\relax
  }
\def\wrap@#1#2#3%
  {%
    \ifnum#1<#2
      0cm #3
      \expandafter\@secondoftwo
    \fi
    \@gobble{\expandafter\wrap@\expandafter{\the\numexpr#1+1}{#2}{#3}}%
  }
\catcode`\@=12

\wrap{6}{10cm} Here is the definitive guide to the use of TeX, written by the
system's creator, Donald E. Knuth. TeX represents the state of the art in
computer typesetting. It is particularly valuable where the document, article,
or book to be produced contains a lot of mathematics, and where the user is
concerned about typographic quality. TeX software offers both writers and
publishers the opportunity to produce technical text of all kinds, in an
attractive form, with the speed and efficiency of a computer system. Novice and
expert users alike will gain from The TeXbook the level of information they
seek. Knuth warns newcomers away from the more difficult areas, while he entices
experienced users with new challenges. The novice need not learn much about TeX
to prepare a simple manuscript with it. But for the preparation of more complex
documents, The TeXbook contains all the detail required. Knuth's familiar wit,
and illustrations specially drawn by Duane Bibby, add a light touch to an
unusually readable software manual. The TeXbook is the first in a five-volume
series on Computers and Typesetting, all authored by Knuth.
\bye

4 sgmoye Dec 21 2020 at 03:02

Pour une application relativement simple comme celle-ci (l'indentation ne s'étend que sur un seul paragraphe), TeX clair propose \hangafter(le nombre de lignes à indenter, positif si de gauche, négatif si de droite, comme ici) et \hangindent(positif si l'indentation commence après le nombre le nombre donné par \hangafter, négatif si commençant à la ligne 1) - voir The TeXbook de Knuth , page 102.

\hangafter=-6 \hangindent=-6cm
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\bye

Juste pour être complet, une simple macro, \wrappourrait être définie (la sortie est identique):

\def\wrap#1#2{\hangafter-#1\hangindent-#2}

\wrap{6}{6cm}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
2 egreg Dec 21 2020 at 03:49

Une simple boucle extensible dans expl3; il est facile de spécifier le nombre souhaité de lignes spéciales au lieu d'être une seule.

\input expl3-generic

\ExplSyntaxOn

\cs_new:Npn \wrap #1 #2
 {
  \parshape
    \int_to_arabic:n { #1 + 1 } \c_space_tl % number of special lines
    \prg_replicate:nn { #1 } { 0pt~#2~ } % parameters for the special lines
    0pt~\hsize % further lines
  \ignorespaces
 }

\ExplSyntaxOff

\wrap{6}{10cm} Here is the definitive guide to the use of TeX, written by 
the system's creator, Donald E. Knuth. TeX represents the state of the art 
in computer typesetting. It is particularly valuable where the document, 
article, or book to be produced contains a lot of mathematics, and where 
the user is concerned about typographic quality. TeX software offers both 
writers and publishers the opportunity to produce technical text of all 
kinds, in an attractive form, with the speed and efficiency of a computer 
system. Novice and expert users alike will gain from The TeXbook the level 
of information they seek. Knuth warns newcomers away from the more difficult 
areas, while he entices experienced users with new challenges. The novice 
need not learn much about TeX to prepare a simple manuscript with it. But 
for the preparation of more complex documents, The TeXbook contains all 
the detail required. Knuth's familiar wit, and illustrations specially 
drawn by Duane Bibby, add a light touch to an unusually readable software 
manual. The TeXbook is the first in a five-volume series on Computers and 
Typesetting, all authored by Knuth

\bye

La macro \wrapest elle-même extensible, vous pouvez donc le faire

\edef\wrapsix{\wrap{6}{10cm}}

\wrapsix Here is the definitive guide to the use of TeX, written by
the system's creator, Donald E. Knuth. TeX represents the state of the art
in computer typesetting. It is particularly valuable where the document,
article, or book to be produced contains a lot of mathematics, and where
[...]

et définir diverses formes de ce type au début.

1 wipet Dec 21 2020 at 05:00

OpTeX prend en charge la \fornumcommande extensible , donc une autre approche consiste à utiliser cette \fonrumcommande à l'intérieur des \parshapeparamètres. Mais OpTeX n'est pas du TeX pur, c'est du TeX plain:

\def\wrap#1#2{\parshape \numexpr#1+1\relax\space \fornum 1..#1\do{0cm#2}0cm\hsize}

\wrap{6}{10cm}\lipsum[1]
\bye
1 wipet Dec 21 2020 at 04:53

Une autre approche consiste à: préparer une liste de \parshapeparamètres avant l' \parshapeexécution. Une telle préparation doit être effectuée par une commande de boucle classique (non extensible).

\newcount\tmpnum
\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\wrap#1#2{\tmpnum=0 \def\parshapelist{}
    \loop \addto\parshapelist{0cm#2}\advance\tmpnum by1
          \ifnum\tmpnum<#1 \repeat
    \advance\tmpnum by1
    \parshape \the\tmpnum\space \parshapelist 0cm\hsize
}

\wrap{6}{10cm} Here is the definitive guide to the use of TeX, written by the
system's creator, Donald E. Knuth. TeX represents the state of the art in
computer typesetting. It is particularly valuable where the document, article,
or book to be produced contains a lot of mathematics, and where the user is
concerned about typographic quality. TeX software offers both writers and
publishers the opportunity to produce technical text of all kinds, in an
attractive form, with the speed and efficiency of a computer system. Novice and
expert users alike will gain from The TeXbook the level of information they
seek. Knuth warns newcomers away from the more difficult areas, while he entices
experienced users with new challenges. The novice need not learn much about TeX
to prepare a simple manuscript with it. But for the preparation of more complex
documents, The TeXbook contains all the detail required. Knuth's familiar wit,
and illustrations specially drawn by Duane Bibby, add a light touch to an
unusually readable software manual. The TeXbook is the first in a five-volume
series on Computers and Typesetting, all authored by Knuth.
\bye

Bien entendu, un tel exemple concret pourrait être résolu \hangafter=-6 \hangindent=-6cmcomme mentionné par sgmoye. Mais je suppose que vous ne donnez cet exemple que pour étudier la matière du \parshapecomportement.

Plus d'explications, comment cela fonctionne: d'abord: \parshapelistest défini comme vide et le texte 0cm10cm (si #2=10cm) est ajouté au \parshapelist. Nous avons 0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm \parshapelistau bout de la boucle. Maintenant, la primitive \parshapeest traitée:

\parshape 7 0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm0cm\hsize

et il fait le vrai travail.

HenriMenke Dec 21 2020 at 02:22

Comme déjà mentionné dans les commentaires, \loop...\repeatn'est pas extensible et \parshapearrête donc de lire la spécification. Cependant, vous pouvez définir une version simple et entièrement extensible de \loopdans Plain TeX en utilisant \romannumeral.

Essentiellement, l'astuce se résume au fait que le plus grand chiffre romain est celui mqui correspond à \romannumeral1000. Par conséquent \romannumeral2000est transformé en mmet ainsi de suite. Nous pouvons (ab) utiliser ceci pour convertir notre variable de boucle \romannumeralen une séquence de met en supprimer une à chaque itération jusqu'à ce qu'il ne reste plus rien.

J'ai appelé la macro \replicate. Ce n'est pas aussi puissant que \loopparce que vous ne pouvez pas accéder au compteur de boucles, mais il est entièrement extensible.

\catcode`@=11

\long\def\@firstoftwo#1#2{#1}
\long\def\@secondoftwo#1#2{#2}
\long\def\@gobble#1{}
\def\@ifx#1#2{%
    \ifx#1#2%
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
}

% replicate
\def\replicate#1{\expandafter\@replicate\romannumeral#1000\relax\relax}
\def\@replicate#1#2#{\@ifx{\relax}{#1}{\@gobble}{\@@replicate{#2}}}
\long\def\@@replicate#1#2{#2\@replicate#1{#2}}

\catcode`@=12

%%% begin document

\def\wrap#1#2{\parshape=#1\replicate{#1}{ 0cm #2}}

\input lorem\par

\wrap{6}{10cm} \input lorem\par

\input lorem\par

\bye