\message:即使有换行符,也会在新行前面添加符号

\message:即使有换行符,也会在新行前面添加符号

如果我做:

\message{^^J! my very long message that will be cut on many lines because LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it^^J}

我会在日志中看到类似这样的内容:

! my very long message that will be cut on many lines because LaTeX for some we
ird reasons like to manually cut the lines instead of letting the terminal do i
t

但我想!在所有行前面添加符号,因为 TeXstudio 只会将以 a 开头的行打印为!错误。如何在日志中创建新行时自动添加符号?

平均能量损失

\documentclass[]{article}


\begin{document}

\message{! my very long message that will be cut on many lines because LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it}


\end{document}

编辑:为了澄清起见,我希望日志包含:

! my very long message that will be cut on many lines because LaTeX for some we
! ird reasons like to manually cut the lines instead of letting the terminal do
! it

(或者如果它可以在单词边界处断开就更好了,但是嘿,我们不要要求太多)

答案1

这里,我引入了\xmessage,它接收扩展消息并在超出设定限制(此处为 40 个字符)的第一个空格处将其拆分。每个新行都以一个!字符开头。

请注意,任何给定的命令序列仅算作\xmessage一个字符。

\documentclass[]{article}
\usepackage{tokcycle}
\newcounter{charcount}
\makeatletter
\newcommand\xmessage[1]{\bgroup%
  \aftertokcycle{\typeout{! \the\cytoks}}%
  \tokcycle{\addcytoks{##1}\stepcounter{charcount}}
  {\processtoks{##1}}
  {\addcytoks{##1}\stepcounter{charcount}}
  {\addcytoks{##1}\stepcounter{charcount}\splitline}{#1}%
  \egroup}
\newcommand\splitline{%
  \ifnum\value{charcount}>40\typeout{! \the\cytoks}%
  \setcounter{charcount}{0}\cytoks{}\fi}
\begin{document}
\xmessage{my very long message that will be cut on many lines because LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it}
\end{document}

在此处输入图片描述

答案2

在空格处分割输入(可能在完全扩展之后,使用*-variant),然后准备消息,计算行的总长度,并决定在max_print_line超出限制时中断。

\documentclass{article}

\ExplSyntaxOn

% get the current length of error lines
\sys_get_shell:nnN { kpsewhich ~ -var-value=error_line } {} \l_tmpa_tl
\int_const:Nn \c__tobiasbora_prefixmessage_limit_int { \l_tmpa_tl }

\NewDocumentCommand{\prefixmessage}{sO{!}m}
 {
  \IfBooleanTF { #1 }
   {
    \tobiasbora_prefixmessage:ne { #2 } { #3 }
   }
   {
    \tobiasbora_prefixmessage:nn { #2 } { #3 }
   }
 }

\seq_new:N \l__tobiasbora_prefixmessage_text_seq
\str_new:N \l__tobiasbora_prefixmessage_text_str
\int_new:N \l__tobiasbora_prefixmessage_len_int

\cs_new_protected:Nn \tobiasbora_prefixmessage:nn
 {
  \seq_set_split:Nne \l__tobiasbora_prefixmessage_text_seq { ~ } { \tl_to_str:n { #2 } }
  % start up
  \str_set:Nn \l__tobiasbora_prefixmessage_text_str { #1 }
  \int_zero:N \l__tobiasbora_prefixmessage_len_int
  % prepare the string to output
  \seq_map_inline:Nn \l__tobiasbora_prefixmessage_text_seq
   {
    \int_add:Nn \l__tobiasbora_prefixmessage_len_int { \str_count:n { ##1 } + 1 }
    \int_compare:nNnTF
     { \l__tobiasbora_prefixmessage_len_int }
     >
     { \c__tobiasbora_prefixmessage_limit_int - \str_count:n { #1 } }
     {
      \str_put_right:Nn \l__tobiasbora_prefixmessage_text_str { ^^J #1 ~ ##1 }
      \int_set:Nn \l__tobiasbora_prefixmessage_len_int { \str_count:n { ##1 } + 1 }
     }
     {
      \str_put_right:Nn \l__tobiasbora_prefixmessage_text_str { ~ ##1 }
     }
   }
  % output
  \iow_term:e { ^^J \l__tobiasbora_prefixmessage_text_str ^^J }
 }
\cs_generate_variant:Nn \tobiasbora_prefixmessage:nn { ne }

\ExplSyntaxOff

\prefixmessage{my very long message that will be cut on many 
  lines because \LaTeX for some weird reasons like to manually
  cut the lines instead of letting the terminal do it}

\def\foo{(something in the middle)}

\prefixmessage*[>>>]{my very long message that will be cut on many 
  lines because LaTeX \foo\space for some weird reasons like to manually
  cut the lines instead of letting the terminal do it}

\stop

终端和日志文件中的输出

! my very long message that will be cut on many lines because \LaTeX for some
! weird reasons like to manually cut the lines instead of letting the terminal
! do it


>>> my very long message that will be cut on many lines because LaTeX
>>> (something in the middle) for some weird reasons like to manually cut the
>>> lines instead of letting the terminal do it

答案3

另一个带有 LaTeX3 的版本:行与行之间用 分隔\robExtPrefixLogMessage(如果您在代码中使用它,请选择其他名称以免与我的 CTAN 库串通),并在其前面加上\robExtPrefixLogMessage

\def\robExtMessageWithPrefixNumberLines{^^J}%
\cs_new:Nn \robExt__message_with_prefix:n {
  \str_set:Nn \l_tmpa_str {#1}  
  \int_compare:nNnTF {\str_count:N \l_tmpa_str + \str_count:N \robExtPrefixLogMessage} > {78} {
    \typeout{\robExtPrefixLogMessage \str_range:Nnn \l_tmpa_str {1} {78 - \str_count:N \robExtPrefixLogMessage}\robExtMessageWithPrefixNumberLines}
    \robExt__message_with_prefix:x {\str_range:Nnn \l_tmpa_str {78 - \str_count:N \robExtPrefixLogMessage + 1} {-1}}
  }{
    \typeout{\robExtPrefixLogMessage \l_tmpa_str \robExtMessageWithPrefixNumberLines}
  }
}
\cs_generate_variant:Nn \robExt__message_with_prefix:n { x }

相关内容