我只是想知道是否有一个包裹(或者某个包的选项,例如\usepackage[showframe]{geometry}
)可以强调/绘制边框(/线条/框架)每个(以及每一个)单词;和/或每个线。
我希望这能与lipsum
(注意到像\uline{\lipsum[1-10]}
[via ulem
package] 这样的操作会导致 LaTeX 崩溃)。正如标题所说,我主要需要它来进行调试(例如,查看双列文档中行不对齐的原因和位置)。
编辑:发现类似的东西宏 - 遍历空格分隔的列表 - TeX - LaTeX - Stack Exchange;
\documentclass{article}
\makeatletter
\def\mboxed#1{%
\@mboxed#1 \@empty
}
\def\@mboxed#1 #2{%
\fbox{#1}\space % fbox here to have a visual test
\ifx #2\@empty\else
\expandafter\@mboxed
\fi
#2%
}
\makeatother
\begin{document}
\def\ptest{Hello there, some words here.}
\mboxed{This should be tested \ptest}
\end{document}
...但是,您会注意到,只有参数中的“逐字”单词\mboxed{}
是单独装箱的;而命令的内容\ptest
则作为一个整体装箱(并且我希望它们也是单独的单词;而是考虑使用\lipsum
);此外,不能保证这个\mboxed
宏不会扰乱排版(如果\mboxed
根本不存在,就会发生这种情况)。
答案1
以下是稍作修改的版本审查多行文本使用 LaTeX:
\documentclass{article}
\begin{document}
\def\ptest{Hello there, some words here.}
\renewcommand{\fboxsep}{1pt}%
\def\boxwords#1{\boxloopword#1 \nil}
\def\boxloopword#1 #2\nil{%
\fbox{\strut#1} % <- Note the space!
\ifx&% #2 is empty, then & equals &
\let\next\relax
\else
\def\next{\expandafter\boxloopword#2\nil}% iterate
\fi
\next\ignorespaces}
\sloppy
\noindent \textbf{Boxed paragraph:} \medskip
\boxwords{Lorem ipsum dolor sit amet, \ptest{} consectetur adipiscing elit. Suspendisse massa lorem, malesuada
ac imperdiet nec, adipiscing ut lorem. Nunc dignissim nibh erat. Phasellus fermentum lectus
ut mi viverra a pulvinar turpis interdum. Maecenas mollis laoreet consequat. Etiam malesuada
ultrices blandit. Praesent sem felis, consectetur eu consectetur id, tempor id quam. Cras id
lectus vitae dolor varius laoreet. Suspendisse nec metus non justo iaculis ultricies quis
vel enim. In porttitor dictum orci sit amet feugiat. Praesent pellentesque odio eu orci
interdum pulvinar. Suspendisse potenti. Mauris lacus lacus, congue tincidunt condimentum
vitae, elementum quis nibh. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Nulla venenatis iaculis mauris, vitae semper neque ullamcorper et.
Integer et sem eu enim egestas pretium. Sed nulla sapien, pretium eget viverra ut, sollicitudin
tempor urna. In accumsan euismod augue at sagittis. Sed molestie tincidunt erat eu suscipit.
Nullam placerat, ipsum a facilisis venenatis, metus est adipiscing augue, at pharetra enim
dui sit amet metus.}
\bigskip
\noindent \textbf{Unboxed paragraph:} \medskip
Lorem ipsum dolor sit amet, \ptest{} consectetur adipiscing elit. Suspendisse massa lorem, malesuada
ac imperdiet nec, adipiscing ut lorem. Nunc dignissim nibh erat. Phasellus fermentum lectus
ut mi viverra a pulvinar turpis interdum. Maecenas mollis laoreet consequat. Etiam malesuada
ultrices blandit. Praesent sem felis, consectetur eu consectetur id, tempor id quam. Cras id
lectus vitae dolor varius laoreet. Suspendisse nec metus non justo iaculis ultricies quis
vel enim. In porttitor dictum orci sit amet feugiat. Praesent pellentesque odio eu orci
interdum pulvinar. Suspendisse potenti. Mauris lacus lacus, congue tincidunt condimentum
vitae, elementum quis nibh. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Nulla venenatis iaculis mauris, vitae semper neque ullamcorper et.
Integer et sem eu enim egestas pretium. Sed nulla sapien, pretium eget viverra ut, sollicitudin
tempor urna. In accumsan euismod augue at sagittis. Sed molestie tincidunt erat eu suscipit.
Nullam placerat, ipsum a facilisis venenatis, metus est adipiscing augue, at pharetra enim
dui sit amet metus.
\end{document}
您会注意到每个段落的排版都不同。使用可\fbox
删除连字符。此外,使用\expandafter\boxloopword
还允许在参数中扩展宏\boxwords
(如您的\ptest
示例)。
也许另一个解决方案是使用soul
包裹(也在同一个链接)将会很有帮助。
答案2
我不知道 LaTeX 解决方案,但我指出了一些 ConTeXt 解决方案,以防有人想复制代码并在 LaTeX 中实现它们。
你可以使用宏来构造每个单词\processwords
。请参阅metafun 手册。
对于给个别单词加下划线,可以使用\underbars
宏;对于给个别行加下划线,可以使用\underbar
宏。
这三个方法都可以很好地处理多段参数。下面是一个显示用法的简单示例:
\let\processword\framed
\starttext
\startTEXpage
\processwords{\input knuth \endgraf}
\blank[big]
\underbars{\input knuth \endgraf}
\blank[big]
\underbar{\input knuth \endgraf}
\stopTEXpage
\stoptext
这使
对于可视化调试,ConTeXt 还提供了一个\showboxes
宏,用于显示页面上的所有框。请参阅 Hans Hagen 1998 年的拖船文章了解详情。下面是一个小例子
\setuppapersize[A7]
\showboxes
\showframe
\starttext
\section {First}
\startitemize[n]
\item One
\item Two
\stopitemize
\stoptext
这使
编辑可以调整 ConTeXt 解决方案以使其在 LaTeX 中工作,但为此您需要复制所使用的不同 ConTeXt 宏的定义。例如,以下是processwords
在 LaTeX 中定义的方法:
\makeatletter
% from syst-ext.mkii
\long\def\ConvertToConstant#1#2#3%
{\edef\@@stringa{\expandafter\detokenize\expandafter{#2}}%
\edef\@@stringb{\expandafter\detokenize\expandafter{#3}}%
#1{\@@stringa}{\@@stringb}}
% from core-fnt.mkii
\def\doprocesswords#1 #2\od
{\ConvertToConstant\doifnot{#1}{}
{\processword{#1} %
% expandafter added
\expandafter\doprocesswords#2 \od}}
% from core-fnt.mkii
\def\processwords#1%
{\doprocesswords#1 \od\unskip}
% from syst-gen.mkii
\long\def\doifnot#1#2%
{\let\donottest\dontprocesstest
\edef\@@stringa{#1}%
\edef\@@stringb{#2}%
\let\donottest\doprocesstest
\ifx\@@stringa\@@stringb
\expandafter\gobbleoneargument
\else
\expandafter\firstofoneargument
\fi}
\long\def\gobbleoneargument#1{}
\long\def\firstofoneargument#1{#1}
\makeatother
\def\ptest{Hello there, some words here.}
然后使用它作为:
\renewcommand{\fboxsep}{1pt}%
\let\processword\fbox
\documentclass{minimal}
\begin{document}
\processwords{Lorem ipsum dolor sit amet, \ptest{} consectetur adipiscing elit.
Suspendisse massa lorem, malesuada ac imperdiet nec, adipiscing ut lorem. Nunc
dignissim nibh erat. Phasellus fermentum lectus ut mi viverra a pulvinar turpis
interdum. Maecenas mollis laoreet consequat. Etiam malesuada ultrices blandit.
Praesent sem felis, consectetur eu consectetur id, tempor id quam. Cras id
lectus vitae dolor varius laoreet. Suspendisse nec metus non justo iaculis
ultricies quis vel enim. In porttitor dictum orci sit amet feugiat. Praesent
pellentesque odio eu orci interdum pulvinar. Suspendisse potenti. Mauris lacus
lacus, congue tincidunt condimentum vitae, elementum quis nibh. Cum sociis
natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla
venenatis iaculis mauris, vitae semper neque ullamcorper et. Integer et sem eu
enim egestas pretium. Sed nulla sapien, pretium eget viverra ut, sollicitudin
tempor urna. In accumsan euismod augue at sagittis. Sed molestie tincidunt erat
eu suscipit. Nullam placerat, ipsum a facilisis venenatis, metus est adipiscing
augue, at pharetra enim dui sit amet metus.}
\end{document}
答案3
这并不完全针对单个单词,但它是相关的(因为它打印出布局框) - 来自LaTeX 图书类别:边距错误的双面文档 #2565797 - VoidCC:
一般来说,要查看布局情况,您可以放入
\usepackage{layout}
序言,然后粘贴\layout
到文档中以获取图表和几何设置列表。
我猜这是内置的......
答案4
感谢编辑@Aditya的回答;最后,我有一个以双列模式可视化 Lipsum 段落的示例。我只是习惯于\let\processword\uline
将单词加下划线而不是加框。下面的示例还同时激活\showmakeup
:
\documentclass[twocolumn]{article}
\usepackage[nopar]{lipsum} % MUST be nopar!
\usepackage[normalem]{ulem} % for \uline underline
% https://tex.stackexchange.com/questions/26806/expanding-edef-a-lipsum-command
\makeatletter
\def\unpacklipsum#1#2#3{%
\count@=#1\relax
\advance\count@\m@ne
\def#3{}%
\loop\ifnum\count@<#2\relax
\advance\count@\@ne
\edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
\repeat}
\makeatother
\makeatletter
% from syst-ext.mkii
\long\def\ConvertToConstant#1#2#3%
{\edef\@@stringa{\expandafter\detokenize\expandafter{#2}}%
\edef\@@stringb{\expandafter\detokenize\expandafter{#3}}%
#1{\@@stringa}{\@@stringb}}
% from core-fnt.mkii
\def\doprocesswords#1 #2\od
{\ConvertToConstant\doifnot{#1}{}
{\processword{#1} %
% expandafter added
\expandafter\doprocesswords#2 \od}}
% from core-fnt.mkii
\def\processwords#1%
{\doprocesswords#1 \od\unskip}
% from syst-gen.mkii
\long\def\doifnot#1#2%
{\let\donottest\dontprocesstest
\edef\@@stringa{#1}%
\edef\@@stringb{#2}%
\let\donottest\doprocesstest
\ifx\@@stringa\@@stringb
\expandafter\gobbleoneargument
\else
\expandafter\firstofoneargument
\fi}
\long\def\gobbleoneargument#1{}
\long\def\firstofoneargument#1{#1}
\makeatother
% -----
\renewcommand{\fboxsep}{1pt}%
% \let\processword\fbox
% \let\processword\underbar
\let\processword\uline
\begin{document}
\let\unexpanded\protected
\input{norm-tex.mkii} % upon \showmakeup - \normalvfil required
\input{supp-vis.mkiv}
\showmakeup
\unpacklipsum{1}{1}{\myflattenedlipsum}
% note: \processwords{\myflattenedlipsum} typesets in single line!
% must have at least a letter and a space, to cause per-word processing
% HOWEVER, \processwords{a \lipsum[1-2]} will *still* be typeset as one line!
% \processwords{a \myflattenedlipsum}
\processwords{P \myflattenedlipsum}
\unpacklipsum{2}{2}{\myflattenedlipsum}
\processwords{P \myflattenedlipsum}
\unpacklipsum{3}{4}{\myflattenedlipsum}
\processwords{P \myflattenedlipsum}
\end{document}
...因此结果如下,渲染效果有些繁重:
....但至少我现在有一个参考点,并且在两列模式下行对齐的视觉指示 - 但代价如下:
\lipsum
段落需要扩展,并且无法使用\par
(因此必须手动重建段落)- 此外,必须在每个段落中添加至少一个字母
\lipsum
(以“解开”单个单词) - 根据连字符的排版不会被保留。
有人会认为应该可以捕获代表给定行/线的 Tex 框并简单地将其 fbox(从而保留连字符),但我尝试查看 \tracingall,却看不到类似的东西 :) 至于复制上下文宏,我尝试在宏中复制\underbars
- 但那个显然有点复杂;在那里我会被看起来像的宏卡住\!!width
,它们显然是上下文特定的(可能适用于 luatex?),因为 Latex 似乎无法解析它们;但我可以看到@Aditya的编辑,!!
可以简单地用 Latex 替换@@
。
没错,我留下之前的例子以供参考(如下) - 但是,它实际上什么也没做;它似乎使用了错误的\underbar
。
好消息是 - Context 可视化宏(supp-vis.mkiv
)确实可以与纯 (la)tex 一起使用;不幸的是,\underbar(s)
每个单词/行下划线的命令是 Context 文件的一部分,这需要大量的 Lua 脚本 - 而且即使使用并运行core-fnt.mkii/mkiv
我也无法欺骗它。\usepackage{luatex}
lualatex
尽管如此,这里有一个 tex 示例,它将使用 Context 可视化宏,并且pdflatex
只能用以下代码进行编译:
\documentclass{article}
\usepackage[nopar]{lipsum}
% https://tex.stackexchange.com/questions/26806/expanding-edef-a-lipsum-command
\makeatletter
\def\unpacklipsum#1#2#3{%
\count@=#1\relax
\advance\count@\m@ne
\def#3{}%
\loop\ifnum\count@<#2\relax
\advance\count@\@ne
\edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
\repeat}
\makeatother
\begin{document}
\let\unexpanded\protected
\input{norm-tex.mkii} % upon \showmakeup - \normalvfil required
\input{supp-vis.mkiv}
\showmakeup
% \showboxes % a bit tinier than \showmakeup
% \showframe % not in supp-vis
\unpacklipsum{1}{1}{\mycommand}
a \mycommand
\end{document}
(输出类似于“可视化调试”\showboxes
宏的示例图像@Aditya的回答)
好的,这是我尝试使用@Aditya在 Latex 中回答;安装 Context ( tlmgr install context
) 后;我只是试图加载它的一些文件“就好像”它们是包一样 - 我得到了一些结果,但只取得了部分成功(可以给文本加下划线;但只能在一行中)...如果有人知道是否不可能采用这种方法,那就太好了。
无论如何,这里是,比如说,' test-con.tex
':
\documentclass{article}
\usepackage[nopar]{lipsum}
% https://tex.stackexchange.com/questions/26806/expanding-edef-a-lipsum-command
\makeatletter
\def\unpacklipsum#1#2#3{%
\count@=#1\relax
\advance\count@\m@ne
\def#3{}%
\loop\ifnum\count@<#2\relax
\advance\count@\@ne
\edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
\repeat}
\makeatother
% \usepackage{contextcopy} % ! LaTeX Error: Missing \begin{document}. at l.740 \newskip \centering \centering....
% same Missing \begin{document} for below as well:
% \makeatletter
% \input{contextcopy.sty}
% \makeatother
\begin{document}
\makeatletter
\input{contextcopy.sty}
\makeatother
\unpacklipsum{1}{1}{\mycommand}
\underbar{Here \mycommand}
\end{document}
...这里是' contextcopy.sty
':
% \input{/path/to/texlive/2011/texmf-dist/tex/context/base/supp-mis.mkii} % auto-read
\input{/path/to/texlive/2011/texmf-dist/tex/context/base/supp-pdf.mkii}
\input{/path/to/texlive/2011/texmf-dist/tex/context/base/syst-ini.mkii}
% \let\unexpanded\normalprotected % as in syst-aux.mkiv gives: ! Undefined control sequence.
% \let\unexpanded\relax %
\let\unexpanded\protect % same effect as \relax
\input{/path/to/texlive/2011/texmf-dist/tex/context/base/supp-vis.mkiv}
有趣的是,即使我使用pdflatex
,在这种设置下,也会dvi
生成:
$ pdflatex test-con.tex
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)
...
) (/path/to/texlive/2011/texmf-dist/tex/context/base/supp-vis.mkiv
loading: ConTeXt Support Macros / Visualization
))
Overfull \hbox (3454.36774pt too wide) in paragraph at lines 133--134
[]$[]$
[1] [2] (./test-con.aux) )
(see the transcript file for additional information)
Output written on test-con.dvi (2 pages, 1292 bytes).
Transcript written on test-con.log.
... 以下是xdvi
它的渲染方式:
显然,如果有可能以某种方式仅从 Context 加载可视化内容,并以其他方式构建 w/plain pdflatex
,以便呈现带下划线的多段文本,那就太棒了:)但我无法判断这种方法最终是否会起作用..
再次感谢大家的回复,
干杯!