如何将宏的字符串参数填充为设定长度的字符串

如何将宏的字符串参数填充为设定长度的字符串

因此,我有一个字符串作为参数传递给宏。此字符串最多可包含 10 个字符,但我需要 latex 来“填充”该字符串,以便它始终包含 10 个字符(通过在末尾添加空格)。如果有人能帮助我,我将不胜感激。

答案1

只要您不介意空格隐式,这里有一种方法。

\documentclass{article}
\usepackage[T1]{fontenc}
\newcounter{tencount}
\makeatletter
{\catcode`\ =12 \gdef\xsp{ }}
\newcommand\maketen{\begingroup\catcode`\ =12 \maketenauxa}
\newcommand\maketenauxa[1]{%
  \setcounter{tencount}{0}\def\tmp{}\maketenauxb#1\@sptoken\@sptoken
  \@sptoken\@sptoken\@sptoken\@sptoken\@sptoken\@sptoken\@sptoken
  \@sptoken\relax}
\def\maketenauxb#1#2\relax{%
  \expandafter\ifx\xsp#1\g@addto@macro\tmp{\@sptoken}\else
    \g@addto@macro\tmp{#1}\fi%
  \stepcounter{tencount}%
  \ifnum\thetencount=10\relax\tmp\expandafter\endgroup\else
  \maketenauxb#2\relax\fi
}
\makeatother
\begin{document}
*\maketen{Hi mom}*

\detokenize\expandafter{\tmp}*
\end{document}

在此处输入图片描述

如果空格需要明确,这里有一个稍微的变化(仅适用于输入流中的字符,但不适用于可扩展宏)。

\documentclass{article}
\usepackage[T1]{fontenc}
\newcounter{tencount}
\makeatletter
\newcommand\XYZ{\expandafter\g@addto@macro\expandafter\tmp\expandafter}
{\catcode`\ =12 \gdef\xsp{ }}
\newcommand\maketen{\begingroup\catcode`\ =12 \maketenauxa}
\newcommand\maketenauxa[1]{%
  \setcounter{tencount}{0}\def\tmp{}\maketenauxb#1\space\space
  \space\space\space\space\space\space\space
  \space\relax}
\def\maketenauxb#1#2\relax{%
  \expandafter\ifx\xsp#1\XYZ{\space}\else
    \XYZ{#1}\fi%
  \stepcounter{tencount}%
  \ifnum\thetencount=10\relax\tmp\expandafter\endgroup\else
  \maketenauxb#2\relax\fi
}
\makeatother
\begin{document}
*\maketen{Hi mom}*

*\detokenize\expandafter{\tmp}*
\end{document}

在此处输入图片描述

答案2

\documentclass{article}

\makeatletter
\newcommand\padtoten[1]{\zzzz{}#1 \\ }
\def\zzzz#1#2 {%
\ifx\\#2%
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
{\zzzz{#1#2\space}}{\zz#1\space\space\space\space\space\space\space\space\space\space\zzz}%
}
\def\zz#1#2#3#4#5#6#7#8#9{#1#2#3#4#5#6#7#8#9\zzz}
\def\zzz#1#2\zzz{#1}

\begin{document}

\typeout{|\padtoten{a}|}
\typeout{|\padtoten{abcdef}|}
\typeout{|\padtoten{a cd f}|}
\typeout{|\padtoten{0123456789}|}

\end{document}

生成日志(只是| |为了使填充可见)。

|a         |
|abcdef    |
|a cd f    |
|0123456789|

答案3

我假设你的“字符串”是可打印字符标记的序列。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\pad}{D(){10}O{~}m}
 {
  #3\prg_replicate:nn { #1 - \tl_count_tokens:n { #3 } } { #2 }
 }
\ExplSyntaxOff

\begin{document}

\typeout{Pad to 10 with spaces}
\typeout{|\pad{a}|}
\typeout{|\pad{abcdef}|}
\typeout{|\pad{a cd f}|}
\typeout{|\pad{0123456789}|}
\typeout{Pad to 10 with !}
\typeout{|\pad[!]{a}|}
\typeout{|\pad[!]{abcdef}|}
\typeout{|\pad[!]{a cd f}|}
\typeout{|\pad{0123456789}|}
\typeout{Pad to 6 with spaces}
\typeout{|\pad(6){a}|}
\typeout{|\pad(6){abcdef}|}
\typeout{|\pad(6){a cd f}|}
\typeout{|\pad(6){012345}|}
\typeout{Pad to 6 with !}
\typeout{|\pad(6)[!]{a}|}
\typeout{|\pad(6)[!]{abcdef}|}
\typeout{|\pad(6)[!]{a cd f}|}
\typeout{|\pad(6){012345}|}

\end{document}

这将在控制台和日志文件中生成

Pad to 10 with spaces
|a         |
|abcdef    |
|a cd f    |
|0123456789|
Pad to 10 with !
|a!!!!!!!!!|
|abcdef!!!!|
|a cd f!!!!|
|0123456789|
Pad to 6 with spaces
|a     |
|abcdef|
|a cd f|
|012345|
Pad to 6 with !
|a!!!!!|
|abcdef|
|a cd f|
|012345|

如果字符串比规定的长度更长,则会引发错误。

答案4

通常,左花括号{和右花括号}在 (La)TeX 中具有特殊含义:
左花括号通常具有类别代码 1(组开头)。
右花括号通常具有类别代码 2(组结尾)。

在正常情况下,明确的组开始字符标记和明确的组结束字符标记不会在输出文件/.pdf 文件中产生可见的输出。

但它们会产生可见的输出,例如,当打印在屏幕上、或写入外部文本文件、或写入 .log 文件、或传递给 时\detokenize

除此之外,无论其类别代码如何,它们都可以出现在由 TeX 原语收集的文件名中,例如\openin\openout或(在 LaTeX 2ε 中\input重命名为)。\@@input

井号 ( #) 在 (La)TeX 中通常也有特殊含义。
井号通常具有类别代码 6(参数)。
通常它不能出现在普通文本中。在普通文本中,您需要改写\#或 之类的内容。井号可以出现在由 TeX 原语(如、或(在 LaTeX 2ε 中重命名为))
收集的文件名中。\openin\openout\input\@@input
类别代码 6(参数)的显式字符标记,即哈希(#),在写入文件或屏幕或传递给\detokenize或时将被复制\scantokens

您没有透露您需要此填充机制的用途。

因此,很难猜测在计算需要附加的空格字符数量时考虑此类井号和花括号是否有意义。

我决定例行

\PadToLengthWithCharacter{⟨flags⟩}%
                         {⟨desired string length⟩}%
                         {⟨character to be used for padding⟩}%
                         {⟨string⟩}

⟨flags⟩必须具有以下值之一00 /  01 /  10 /  11 。

如果⟨flags⟩= 00,则类别代码 6(参数)的显式字符标记将算作单个字符(即,不考虑哈希加倍),并且类别代码 1(组开始)各自 2(组结束)的一对匹配的显式字符标记根本不会算作一个字符。

如果⟨flags⟩= 01,则类别代码 6(参数)的显式字符标记将算作单个字符(即,不考虑哈希加倍),并且类别代码 1(组开头)和 2(组结尾)的一对匹配的显式字符标记将算作两个字符。

如果⟨flags⟩= 10,则类别代码 6(参数)的显式字符标记将算作两个字符(即,考虑到哈希加倍),并且类别代码 1(组开始)各自 2(组结束)的一对匹配的显式字符标记根本不会算作一个字符。

如果⟨flags⟩= 11,则类别代码 6(参数)的显式字符标记将算作两个字符(即,考虑到哈希加倍),并且类别代码 1(组开始)各自 2(组结束)的一对匹配的显式字符标记将算作两个字符。

该例程需要\detokenizeε-TeX 扩展来检测哈希。\string#产生类别代码为 12(其他)的单个哈希值。\detokenize{#}产生类别代码为 12(其他)的两个哈希值。)

如果⟨string⟩的字符数少于⟨desired string length⟩,则⟨character to be used for padding⟩使用 进行填充。否则⟨string⟩保持原样。

该例程是可扩展的,即可以在完全扩展上下文中使用。由于\romannumeral0扩展,结果在两个扩展步骤后交付。

该例程不会扩展⟨string⟩以检测⟨string⟩的长度。实际上,为了检测 的⟨string⟩长度,该例程会计算标记,而不仅仅是字符。并且该例程假设 -argument⟨character to be used for padding⟩产生单个字符。没有对此实施错误检查。

例如,如果⟨string⟩= \textbf{#123},那么

  • 其中⟨flags⟩= 00⟨string⟩假设 的长度=5:
    • \textbf被视为字符串的一个字符。
    • {不被视为字符串的字符。
    • #被视为字符串的一个字符。
    • 1被视为字符串的一个字符。
    • 2被视为字符串的一个字符。
    • 3被视为字符串的一个字符。
    • }不被视为字符串的字符。
  • 其中⟨flags⟩= 01⟨string⟩假设 的长度=7:
    • \textbf被视为字符串的一个字符。
    • {被视为字符串的一个字符。
    • #被视为字符串的一个字符。
    • 1被视为字符串的一个字符。
    • 2被视为字符串的一个字符。
    • 3被视为字符串的一个字符。
    • }被视为字符串的一个字符。
  • 其中⟨flags⟩= 10⟨string⟩假设 的长度=6:
    • \textbf被视为字符串的一个字符。
    • {不被视为字符串的字符。
    • #取字符串的两个字符。
    • 1被视为字符串的一个字符。
    • 2被视为字符串的一个字符。
    • 3被视为字符串的一个字符。
    • }不被视为字符串的字符。
  • 其中⟨flags⟩= 11⟨string⟩假设 的长度=8:
    • \textbf被视为字符串的一个字符。
    • {被视为字符串的一个字符。
    • #取字符串的两个字符。
    • 1被视为字符串的一个字符。
    • 2被视为字符串的一个字符。
    • 3被视为字符串的一个字符。
    • }被视为字符串的一个字符。

包含例程的示例:

\documentclass[landscape]{article}

\makeatletter
%%=============================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo,
%%    \UD@Exchange, \UD@PassFirstBehindThirdToSecond
%%    \UD@removespace, \UD@CheckWhetherNull, \UD@CheckWhetherBrace,
%%    \UD@CheckWhetherLeadingSpace, \UD@CheckWhetherHash, 
%%    \UD@ExtractFirstArg
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@Exchange[2]{#2#1}%
\newcommand\UD@PassFirstBehindThirdToSecond[3]{#2{#3}{#1}}%
\@ifdefinable\UD@removespace{\UD@firstoftwo{\def\UD@removespace}{} {}}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\UD@firstoftwo\expandafter{} \UD@secondoftwo}%
  {\UD@firstoftwo\expandafter{} \UD@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument's first token is a catcode-1-character
%%.............................................................................
%% \UD@CheckWhetherBrace{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has leading
%%                        catcode-1-token>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has no leading
%%                        catcode-1-token>}%
\newcommand\UD@CheckWhetherBrace[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\expandafter{\expandafter{%
  \string#1.}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\UD@firstoftwo\expandafter{} \UD@firstoftwo}%
  {\UD@firstoftwo\expandafter{} \UD@secondoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether brace-balanced argument's first token is an explicit
%% space token
%%.............................................................................
%% \UD@CheckWhetherLeadingSpace{<Argument which is to be checked>}%
%%                             {<Tokens to be delivered in case <argument
%%                               which is to be checked>'s 1st token is a
%%                               space-token>}%
%%                             {<Tokens to be delivered in case <argument
%%                               which is to be checked>'s 1st token is not
%%                               a space-token>}%
\newcommand\UD@CheckWhetherLeadingSpace[1]{%
  \romannumeral0\UD@CheckWhetherNull{#1}%
  {\UD@firstoftwo\expandafter{} \UD@secondoftwo}%
  {\expandafter\UD@secondoftwo\string{\UD@@CheckWhetherLeadingSpace.#1 }{}}%
}%
\@ifdefinable\UD@@CheckWhetherLeadingSpace{%
  \long\def\UD@@CheckWhetherLeadingSpace#1 {%
    \expandafter\UD@CheckWhetherNull\expandafter{\UD@secondoftwo#1{}}%
    {\UD@Exchange{\UD@firstoftwo}}{\UD@Exchange{\UD@secondoftwo}}%
    {\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter
     \expandafter\expandafter\expandafter}\expandafter\expandafter
     \expandafter}\expandafter\UD@secondoftwo\expandafter{\string}%
  }%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument (which mut be a single token!!!!) is an
%% explicit character token of category code 6(parameter)
%%.............................................................................
%% \UD@CheckWhetherHash{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case <argument which is to
%%                       be checked> is an explicit character token of category
%%                       code 6>}%
%%                     {<Tokens to be delivered in case <argument which is to
%%                       be checked> is not an explicit character token of
%%                       category code 6>}%
\newcommand\UD@CheckWhetherHash[1]{%
  \romannumeral0%
  \expandafter\UD@CheckWhetherLeadingSpace\expandafter{\string#1}{%
    \expandafter\expandafter\expandafter\UD@CheckWhetherNull
    \expandafter\expandafter\expandafter{%
    \expandafter\UD@removespace\string#1}{%
      \expandafter\expandafter\expandafter\UD@CheckWhetherNull
      \expandafter\expandafter\expandafter{%
      \expandafter\UD@removespace\detokenize{#1}}{%
        % space of whatsoever catcode
        \UD@firstoftwo\expandafter{} \UD@secondoftwo
      }{% space of catcode 6
        \UD@firstoftwo\expandafter{} \UD@firstoftwo
      }%
    }{% space of whatsoever catcode
      \UD@firstoftwo\expandafter{} \UD@secondoftwo
    }%
  }{%
    \expandafter\expandafter\expandafter\UD@CheckWhetherNull
    \expandafter\expandafter\expandafter{%
    \expandafter\UD@firstoftwo
    \expandafter{\expandafter}\string#1}{%
      \expandafter\expandafter\expandafter\UD@CheckWhetherNull
      \expandafter\expandafter\expandafter{%
      \expandafter\UD@firstoftwo
      \expandafter{\expandafter}\detokenize{#1}}{%
        % no hash
        \UD@firstoftwo\expandafter{} \UD@secondoftwo
      }{% hash
        \UD@firstoftwo\expandafter{} \UD@firstoftwo
      }%
    }{% no hash
      \UD@firstoftwo\expandafter{} \UD@secondoftwo
    }%
  }%
}%
%%-----------------------------------------------------------------------------
%% Extract first inner undelimited argument:
%%
%%   \UD@ExtractFirstArg{ABCDE} yields  {A}
%%
%%   \UD@ExtractFirstArg{{AB}CDE} yields  {AB}
%%.............................................................................
\@ifdefinable\UD@RemoveTillUD@SelDOm{%
  \long\def\UD@RemoveTillUD@SelDOm#1#2\UD@SelDOm{{#1}}%
}%
\newcommand\UD@ExtractFirstArg[1]{%
  \romannumeral0%
  \UD@ExtractFirstArgLoop{#1\UD@SelDOm}%
}%
\newcommand\UD@ExtractFirstArgLoop[1]{%
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
  { #1}%
  {\expandafter\UD@ExtractFirstArgLoop\expandafter{\UD@RemoveTillUD@SelDOm#1}}%
}%
%%-----------------------------------------------------------------------------
%% Fork whether argument is the token "1" or is not token "1":
%%.............................................................................
\@ifdefinable\UD@GobbleToExclam{\long\def\UD@GobbleToExclam#1!{}}%
\newcommand\UD@CheckWhetherNoExclam[1]{%
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@GobbleToExclam#1!}%
}%
\@ifdefinable\UD@OneSelect{\long\def\UD@OneSelect#1!0!1!#2#3!!{#2}}%
\newcommand\UD@OneFork[1]{%
  \romannumeral0%
  \UD@CheckWhetherNoExclam{#1}{%
    \UD@OneSelect
    !#1!1!{\UD@firstoftwo\expandafter{} \UD@secondoftwo}%
    !0!#1!{\UD@firstoftwo\expandafter{} \UD@firstoftwo}%
    !0!1!{\UD@firstoftwo\expandafter{} \UD@secondoftwo}%
    !!%
  }{\UD@firstoftwo\expandafter{} \UD@secondoftwo}%
}%
%%=============================================================================
\newcommand\PadToLengthWithCharacter[4]{%
  % #1 = <flags> 
  % #2 = <desired string length>
  % #3 = <character to be used for padding>
  % #4 = <string>
  \romannumeral0%
  \expandafter\UD@PassFirstBehindThirdToSecond
  \expandafter{\romannumeral\number\number#2 000}%
              {\UD@secondoftwo{}}%
              {\UD@PadToLengthWithCharacterLoop{#1}{#3}{#4}}{#4}%
}%
%%=============================================================================
\newcommand\UD@PadToLengthWithCharacterLoop[5]{%
    % #1 = <flags>
    % #2 = <character to be used for padding>
    % #3 = <string where things get removed>
    % #4 = <amount of letters m denoting (remaining) desired string length>
    % #5 = <string where things get added>
  \UD@CheckWhetherNull{#4}{ #5}{%
    \UD@CheckWhetherNull{#3}{%
      \UD@PassFirstBehindThirdToSecond{#5#2}{%
        \expandafter\UD@PassFirstBehindThirdToSecond
        \expandafter{\UD@firstoftwo{}#4}{%
          \UD@PassFirstBehindThirdToSecond{}{\UD@secondoftwo{}}%
        }%
      }%
    }{%
      \UD@PassFirstBehindThirdToSecond{#5}{%
        \UD@CheckWhetherLeadingSpace{#3}{%
          \expandafter\UD@PassFirstBehindThirdToSecond
          \expandafter{\UD@firstoftwo{}#4}{%
            \expandafter\UD@PassFirstBehindThirdToSecond
            \expandafter{\UD@removespace#3}%
            {\UD@secondoftwo{}}%
          }%
        }{%
          \UD@CheckWhetherBrace{#3}{%
            \expandafter\UD@OneFork\expandafter{\UD@secondoftwo#1}{%
              \expandafter\UD@CheckWhetherNull
              \expandafter{\UD@firstoftwo{}#4}%
              {\UD@PassFirstBehindThirdToSecond{}}{%
                \expandafter\expandafter
                \expandafter\UD@PassFirstBehindThirdToSecond
                \expandafter\expandafter
                \expandafter{\UD@firstoftwo{\UD@firstoftwo{}}#4}%
              }%
            }{\UD@PassFirstBehindThirdToSecond{#4}}%
            {%
              \expandafter\UD@PassFirstBehindThirdToSecond
              \expandafter{\UD@secondoftwo{}#3}{\UD@secondoftwo{}}%
            }%
          }{%
            \expandafter\UD@CheckWhetherHash
            \romannumeral0\UD@ExtractFirstArgLoop{#3\UD@SelDOm}{%
              \expandafter\UD@OneFork\expandafter{\UD@firstoftwo#1}{%
                \UD@firstoftwo{%
                  \expandafter\UD@CheckWhetherNull
                  \expandafter{\UD@firstoftwo{}#4}%
                  {\UD@PassFirstBehindThirdToSecond{}}{%
                    \expandafter\expandafter
                    \expandafter\UD@PassFirstBehindThirdToSecond
                    \expandafter\expandafter
                    \expandafter{\UD@firstoftwo{\UD@firstoftwo{}}#4}%
                  }%
                }%
              }{\UD@secondoftwo{}}%
            }{\UD@secondoftwo{}}%
            {%
              \expandafter\UD@PassFirstBehindThirdToSecond
              \expandafter{\UD@firstoftwo{}#4}}{%
                \expandafter\UD@PassFirstBehindThirdToSecond
                \expandafter{\UD@firstoftwo{}#3}%
              {\UD@secondoftwo{}}%
            }%
          }%
        }%
      }%
    }%
    {\UD@PadToLengthWithCharacterLoop{#1}{#2}}%
  }%
}%
%%=============================================================================
\makeatother

\makeatletter
%%==== This section of code is only for decorating the text with a grid: ======
%% Draw quads for characters of \ttfamily-font:
%%    \Quadpaperphantom{<columns>}{<rows>}{<background color>}{<grid color>}
%%-----------------------------------------------------------------------------
\usepackage{color}
\newbox\mybox
\newskip\UD@GridruleThickness
\UD@GridruleThickness=0.4pt\relax
\newcommand\Quadpaperphantom[4]{%
  \setbox\mybox=\vbox{%
    {%
      \normalfont\ttfamily\selectfont
      \kern\dp\strutbox
      \fboxsep=-\UD@GridruleThickness
      \fboxrule=\UD@GridruleThickness % <-width of frame around the whole grid
      \kern-\fboxrule
      \kern-\fboxsep
      \noindent
      \kern-.5\UD@GridruleThickness
      \kern-\fboxsep
      \kern-\fboxrule
      \@tempdima=#2\baselineskip
      \addtolength\@tempdima{\UD@GridruleThickness}%
      \edef\tempa{\the\@tempdima}%
      \settowidth\@tempdima{X}%
      \@tempdima=#1\@tempdima
      \addtolength\@tempdima{\UD@GridruleThickness}%
      \edef\tempb{\the\@tempdima}%
      \@ifundefined{color}{}{\color#3}%
      \fbox{%
        \fboxsep=0pt
        \fboxrule=0pt
        \@ifundefined{colorbox}{\mbox}{\colorbox#4}%
        {%
          \parbox[b][\tempa]{\tempb}{%
            \@ifundefined{color}{}{\color#3}%
            \@tempdima=\baselineskip
            \addtolength\@tempdima{-\UD@GridruleThickness}%
            \edef\tempa{\the\@tempdima}%
            \offinterlineskip
            \leavevmode\vskip0pt%
            \leaders\vbox{%
              \noindent\hrule height \UD@GridruleThickness depth 0pt%
              \noindent\hbox to\hsize{%
                \leaders\hbox{%
                  \vrule height\tempa depth 0pt width \UD@GridruleThickness
                  \settowidth\@tempdima{X}%
                  \addtolength\@tempdima{-\UD@GridruleThickness}%
                  \kern\@tempdima\relax
                }\hfill\vrule height\tempa depth 0pt width \UD@GridruleThickness
              }%
            }\vfill
            \hrule height \UD@GridruleThickness depth 0pt%
          }%
        }%
      }%
    }%
  }%
  \ht\mybox=0pt
  \dp\mybox=0pt
  \wd\mybox=0pt
  \box\mybox
}%
%%=============================================================================
\makeatother

%-------------------[adjust margins/layout for the example]--------------------
\csname @ifundefined\endcsname{pagewidth}{}{\pagewidth=\paperwidth}%
\csname @ifundefined\endcsname{pdfpagewidth}{}{\pdfpagewidth=\paperwidth}%
\csname @ifundefined\endcsname{pageheight}{}{\pageheight=\paperheight}%
\csname @ifundefined\endcsname{pdfpageheight}{}{\pdfpageheight=\paperheight}%
\textwidth=\paperwidth
\oddsidemargin=2cm
\marginparsep=.125\oddsidemargin
\marginparwidth=\oddsidemargin
\advance\marginparwidth-2\marginparsep
\advance\textwidth-2\oddsidemargin
\advance\oddsidemargin-1in
\evensidemargin=\oddsidemargin
\textheight=\paperheight
\topmargin=2cm
\footskip=.5\topmargin
{\normalfont\global\advance\footskip.5\ht\strutbox}%
\advance\textheight-2\topmargin
\advance\topmargin-1in
\headheight=0ex
\headsep=0ex
\pagestyle{plain}
%------------------[eof margin-adjustments]------------------------------------

\parskip=0ex
\parindent=0ex

\begin{document}
\ttfamily\frenchspacing\sloppy

% Let's draw some quads for making counting amount of characters more easy:
\Quadpaperphantom{129}{38}{[cmyk]{0, 0, 0, 0.4}}{[rgb]{1, 1, 1}}%

% Let's write some numbers for making counting amount of characters more easy:
\textcolor{red}{123456789012345678901234567890123456789012345678901234567890%
123456789012345678901234567890123456789012345678901234567890123456789}\\%

Under normal circumstances curly braces are not visible and \verb|#| cannot be
used---therefore flags=00 is sufficient:\\

\verb|\PadToLengthWithCharacter{00}{10}{A}{123}X| yields:\\
\PadToLengthWithCharacter{00}{10}{A}{123}X%
\\

\verb|\PadToLengthWithCharacter{00}{10}{A}{1234567890}X| yields:\\
\PadToLengthWithCharacter{00}{10}{A}{1234567890}X%
\\

\verb|\PadToLengthWithCharacter{00}{10}{ }{{{1{23}4}5}}X| yields:\\
\PadToLengthWithCharacter{00}{10}{ }{{{1{23}4}5}}X%
\\

\verb|\PadToLengthWithCharacter{00}{10}{A}{1{23{456{7}8}9}0}X| yields:\\
\PadToLengthWithCharacter{00}{10}{A}{1{23{456{7}8}9}0}X%
\\

\verb|\PadToLengthWithCharacter{00}{10}{A}{1{23{456{7}8}9}01}X| yields:\\
\PadToLengthWithCharacter{00}{10}{A}{1{23{456{7}8}9}01}X%
\\

\verb|\PadToLengthWithCharacter{00}{10}{A}{12345678901}X| yields:\\
\PadToLengthWithCharacter{00}{10}{A}{12345678901}X%
\\

\verb|\PadToLengthWithCharacter{00}{10}{ }{}X| yields:\\
% Be aware that horizontal glue coming from spces will be removed at
% beginnings of lines, thus the \null-box:
\null\PadToLengthWithCharacter{00}{10}{ }{}X%
\\

When things are written to screen, curly braces are visible and \verb|#| gets doubled. Same with \verb|\detokenize|.\\
Therefore in such situations flags=11 might be useful:\\

\verb|\detokenize\expandafter\expandafter\expandafter{\PadToLengthWithCharacter{11}{10}{A}{A#{56}}X}| yields:\\
\detokenize\expandafter\expandafter\expandafter{\PadToLengthWithCharacter{11}{10}{A}{A#{56}}X}%
\\

\verb|\detokenize\expandafter\expandafter\expandafter{\PadToLengthWithCharacter{11}{15}{ }{A#{56}}X}| yields:\\
\detokenize\expandafter\expandafter\expandafter{\PadToLengthWithCharacter{11}{15}{ }{A#{56}}X}%
\\

\verb|\detokenize\expandafter\expandafter\expandafter{\PadToLengthWithCharacter{11}{20}{ }{A{#{56}8} 1}X}| yields:\\
\detokenize\expandafter\expandafter\expandafter{\PadToLengthWithCharacter{11}{20}{ }{A{#{56}8} 1}X}%
\\

% Let's write some numbers for making counting amount of characters more easy:
\textcolor{red}{123456789012345678901234567890123456789012345678901234567890%
123456789012345678901234567890123456789012345678901234567890123456789}\\%
\end{document}

在此处输入图片描述

相关内容