Plain TeX: Sự cố khi \ vòng lặp để thiết lập các tham số cho \ parshape
Tôi đang cố gắng viết macro để gói văn bản xung quanh một hình. Tôi biết rằng nó có sẵn trong Latex và có một gói khác, figflow dành cho trơnTex. Tôi chỉ đang học cách viết của riêng tôi. Bước đầu tiên của tôi là sử dụng \ parshape để tạo vùng trống cho hình. Tôi bị mắc kẹt ở đây. Tôi xác định macro như sau:
\newcount\ln
\def\wrap#1#2{\parshape=#1 \loop\relax\ifnum\ln<#1 0cm #2 \advance\ln by 1\repeat}
Nó không hoạt động. Tôi đã thử \edef, mà cũng không hoạt động. Xin vui lòng bất cứ ai có thể giúp đỡ? Cảm ơn!
\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
Trả lời
Một câu trả lời cho thấy một vòng lặp khá cơ bản với \numexpr.
Khi bạn đang sử dụng \parshapevà chỉ muốn một số dòng cụ thể bị ảnh hưởng, bạn phải thêm một dòng bổ sung 0cm \hsizeđể hoàn nguyên về các dòng bình thường sau khi \parshapehoàn tất, vì \parshapesẽ lặp lại đặc điểm kỹ thuật cuối cùng cho mọi dòng còn lại của đoạn.
\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
Đối với một ứng dụng tương đối đơn giản như thế này (thụt lề chỉ kéo dài trên một đoạn văn), TeX đơn giản cung cấp \hangafter(số dòng để thụt lề, dương nếu từ trái, âm nếu từ phải, như ở đây) và \hangindent(dương nếu thụt vào bắt đầu sau số mà số được cho \hangafter, âm nếu bắt đầu từ dòng 1) - xem Knuth's The TeXbook , trang 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
Để hoàn chỉnh, một macro đơn giản, \wrapcó thể được xác định (đầu ra giống hệt nhau):
\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.
Một vòng lặp có thể mở rộng đơn giản trong expl3; thật dễ dàng để chỉ định số dòng đặc biệt mong muốn thay vì chỉ một dòng.
\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
Bản thân macro \wrapcó thể mở rộng, vì vậy bạn có thể làm
\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
[...]
và xác định các hình dạng khác nhau của loại này ngay từ đầu.
OpTeX hỗ trợ \fornumlệnh có thể mở rộng , vì vậy một cách tiếp cận khác là sử dụng \fonrumlệnh này bên trong \parshapecác tham số. Nhưng OpTeX không phải là TeX đơn thuần, nó là TeX đơn giản mở rộng:
\def\wrap#1#2{\parshape \numexpr#1+1\relax\space \fornum 1..#1\do{0cm#2}0cm\hsize}
\wrap{6}{10cm}\lipsum[1]
\bye
Một cách tiếp cận khác là: chuẩn bị một danh sách các \parshapetham số trước khi \parshapechạy. Việc chuẩn bị như vậy nên được thực hiện bằng lệnh lặp cổ điển (không thể mở rộng).
\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
Tất nhiên, ví dụ cụ thể như vậy có thể được giải quyết bằng \hangafter=-6 \hangindent=-6cmcách được đề cập bởi sgmoye. Nhưng tôi cho rằng bạn đưa ra ví dụ này chỉ để làm tài liệu nghiên cứu về \parshapehành vi.
Giải thích thêm, cách hoạt động: thứ nhất: \parshapelistđược định nghĩa là trống và văn bản 0cm10cm (if #2=10cm) được nối vào \parshapelist. Chúng ta có 0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm ở \parshapelistcuối vòng lặp. Bây giờ nguyên thủy \parshapeđược xử lý:
\parshape 7 0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm0cm10cm0cm\hsize
và nó hoạt động thực sự.
Như đã được đề cập trong các nhận xét, \loop...\repeatkhông thể mở rộng và do đó \parshapengừng đọc thông số kỹ thuật. Tuy nhiên, bạn có thể xác định một phiên bản có thể mở rộng hoàn toàn đơn giản của \looptrong Plain TeX bằng cách sử dụng \romannumeral.
Về cơ bản, thủ thuật tóm tắt là chữ số la mã lớn nhất mtương ứng với \romannumeral1000. Do đó \romannumeral2000được biến thành mmvà như vậy. Chúng ta có thể (ab) sử dụng điều này để chuyển đổi biến vòng lặp của chúng ta bằng cách sử dụng \romannumeralthành một chuỗi mvà loại bỏ một biến trong mỗi lần lặp cho đến khi không còn lại gì.
Tôi đã gọi macro \replicate. Nó không mạnh bằng \loopvì bạn không thể truy cập bộ đếm vòng lặp nhưng nó hoàn toàn có thể mở rộng.
\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