\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,它採用擴展訊息並在超過設定限制(此處為 4o 個字元)的第一個空格處將其中斷。每個新行都以一個字元開頭!

請注意,任何給定的命令序列\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 }

相關內容