如何使用自定义命令等简化排版内容?

发布于 2022-10-29 12:21:50

由于工作需要,我在排版内容时,一篇文档的段落里经常要紧随文字输入迷你表格(格子仅一行,但数量是一格或多格,不超过8格,里面输入任意字母或数字)。每个要输入多行代码,显得冗长。如何使用自定义命令或宏包,等简化排版内容呢?

\documentclass[]{article}
\begin{document}
    The main purpose of this standard is to coordinate the data exchange and enterprise data sharing of product design and manufacturing CAD in the computer software environment in the manufacturing industry.
    \raisebox{0.3ex}{
    \begin{tabular}{|p{0.05cm}|p{0.72cm}|p{0.62cm}|}
        \hline
        \raisebox{-0.3ex}{A}
        &\raisebox{-0.3ex}{0.25M}
        &\raisebox{-0.3ex}{B}\\
        \cline{1-3}
    \end{tabular}
}
    The main purpose of this standard is to coordinate the data exchange and enterprise data sharing of product design and manufacturing CAD in the computer software environment in the manufacturing industry.\\
    
        The main purpose of this standard    \raisebox{0.3ex}{
            \begin{tabular}{|p{0.05cm}|p{0.5cm}|}
                \hline
                \raisebox{-0.3ex}{E}
                &\raisebox{-0.3ex}{0.8}\\
                \cline{1-2}
            \end{tabular}
        } is to coordinate the data exchange and enterprise data sharing of product design and manufacturing CAD in the computer software environment in the manufacturing industry.

    The main purpose of this standard is to coordinate the data            
    \begin{tabular}{|p{0.05cm}|p{0.5cm}|p{0.25cm}|p{0.25cm}|}
        \hline
        \raisebox{-0.3ex}{E}
        &\raisebox{-0.3ex}{0.8}
        &\raisebox{-0.3ex}{H}
        &\raisebox{-0.3ex}{J}\\
        \cline{1-4}
    \end{tabular} exchange and enterprise data sharing of product design and manufacturing CAD in the computer software environment in the manufacturing industry.
\end{document}

q15.jpg

查看更多

关注者
0
被浏览
974
3 个回答
其实有点卡
其实有点卡 2022-10-29
这家伙很懒,什么也没写!

这里给一个不完善的实验性例子。

首先大可写个循环而不伤身体:

\documentclass[a5paper]{article}
\makeatletter
\def\box@sep{\hskip-.4pt\relax}
\newcommand\minitab[1]{%
  \@for\@box:=#1\do{%
    \box@sep\framebox{\@box}%
  }%
}
\makeatother
\begin{document}
I can eat glass, it \minitab{doesn't,hurt} me.
\end{document}

image.png

上述实现有两个问题:一是\minitab命令一执行就会左移-0.4pt(虽然几乎看不出来),二是盒子高度没有固定。可以在\minitab定义之前加入如

\def\@@box@sep{\let\@box@sep\box@sep}
\let\@box@sep\@@box@sep

这样的魔法来解决第一个问题,至于第二个问题大可用\vphantom去撑(固定盒子高度肯定有更好的方法)。这样我们就可以得到期待的结果了。

\documentclass[a5paper]{article}
\makeatletter
\def\box@sep{\hskip-.4pt\relax}
\def\@@box@sep{\let\@box@sep\box@sep}
\let\@box@sep\@@box@sep
\newcommand\minitab[1]{%
  \@for\@box:=#1\do{%
    \@box@sep\framebox{\vphantom{O'g}\@box}%
  }%
}
\makeatother
\begin{document}
I can eat glass, it \minitab{doesn't,hurt} me.
I can eat glass, \minitab{it,doesn't, hurt,me}.
\end{document}

image.png
它甚至还能换行。。。

当然这是一个实验性的例子,肯定有更好的方法完善代码。不过这个例子有趣的地方在于,实现了参数数目可变的命令。

寄存器
寄存器 2022-10-29
这家伙很懒,什么也没写!

用l3结合coffin也可以实现,不过细节上这里没有做太多考虑:

\documentclass{ctexart}

\usepackage{expl3, xparse}

\ExplSyntaxOn
\cs_new_nopar:Npn \__coffin_ht_plus_dp:N #1
  {
    \coffin_ht:N #1 + \coffin_dp:N #1
  }

\cs_new:Npn \__boxed_txt:n #1
  {
    \hcoffin_set:Nn \l_tmpb_coffin{text}

    \dim_set:Nn \l_tmpa_dim
      { \__coffin_ht_plus_dp:N \l_tmpb_coffin }

    \clist_set:Nn \l_tmpa_clist{ #1 }
    \clist_map_inline:Nn \l_tmpa_clist
      {
        \coffin_clear:N \l_tmpa_coffin
        \hcoffin_set:Nn \l_tmpa_coffin { ##1 }
        \dim_set:Nn \l_tmpb_dim
          { \__coffin_ht_plus_dp:N \l_tmpa_coffin }
        \coffin_scale:Nnn \l_tmpa_coffin
                {
                  \dim_ratio:nn { \l_tmpa_dim } { \l_tmpb_dim }
                }{
                  \dim_ratio:nn { \l_tmpa_dim } { \l_tmpb_dim }
                }
        \fbox{ \coffin_typeset:Nnnnn \l_tmpa_coffin
            { l } { b } { 0pt } { 0pt }
          }
      }
  }

\NewDocumentCommand \inlinebox { m }
  {
    \group_begin:
    \__boxed_txt:n { #1 }
    \group_end:
  }

\ExplSyntaxOff

\begin{document}

I can eat glass, it \inlinebox{doesn't, hurt, {$a^2+b^2=c^2$}} me.

\end{document}

image.png

Swit
Swit 2022-10-29
LaTeX nubility!

给一种思路供参考

\documentclass{article}
\newcounter{tabucolumn}
\NewDocumentCommand\minitabu{m m}%
    {\setcounter{tabucolumn}{#1}
     \begin{tabular}{|*{\value{tabucolumn}}{c|}}%
         \hline #2\\\hline
     \end{tabular}}

\begin{document}

\minitabu{2}{a&b}
\minitabu{3}{$f(x)$&$g(x)$&$h(x)$}
\minitabu{4}{1&2&3&4}

\end{document}

94def8b941f7551e1db5da8ee1686a0c.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览