以下代码在控制台中写入,但不打印空格字符。如何强制打印空格字符?
\documentclass{article}
\begin{document}
\message{%
^^J
=======================================
^^J
[This should be centered]
^^J
=======================================
^^J
}
\
\end{document}
答案1
latex 格式的消息使用\space
一个空格和\@spaces
4 个空格,因此
=======================================
[This should be centered]
=======================================
从
\documentclass{article}
\begin{document}
\makeatletter
\message{%
^^J
=======================================
^^J
\@spaces\space\space\space [This should be centered]
^^J
=======================================
^^J
}
\makeatother
\end{document}
答案2
答案3
它确实会打印空格,但与 TeX 一样,如果您连续输入多个空格,它会将它们折叠为一个空格,并且行首的空格也会被忽略,因此如果您只是在那里输入空格,TeX 会忽略它们。有很多方法可以做到这一点;其中之一是使用来\prg_replicate:nn
放置任意数量的空格:
\documentclass{article}
\ExplSyntaxOn
\cs_new:Npn \spaces #1
{ \prg_replicate:nn {#1} { ~ } }
\ExplSyntaxOff
\begin{document}
\message{%
^^J
=======================================
^^J
\spaces{7}[This should be centered]
^^J
=======================================
^^J
}
\end{document}
答案4
摘要版本:
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\centermessage}{O{78}m}
{
^^J
\iow_term:x { \prg_replicate:nn { #1 } { = } }
\seq_set_split:Nnn \l_tmpa_seq { \\ } { #2 }
\seq_map_tokens:Nn \l_tmpa_seq { \__colas_centerline:nn { #1 } }
\iow_term:x { \prg_replicate:nn { #1 } { = } }
^^J
}
\cs_new:Nn \__colas_centerline:nn
{
\iow_term:x
{
\prg_replicate:nn { \int_div_truncate:nn { #1 - \str_count:n { #2 } } { 2 } } { ~ }
#2
}
}
\ExplSyntaxOff
\begin{document}
\centermessage{
[This should be centered] \\ [and also this]
}
\centermessage[50]{
[This should be centered] \\ [and also this]
}
\end{document}
这是你将在控制台中看到的内容
==============================================================================
[This should be centered]
[and also this]
==============================================================================
==================================================
[This should be centered]
[and also this]
==================================================