特定表格格式的宏

特定表格格式的宏

现在我需要(对于一个项目)一个功能,它看起来像\PhysicsProblem{Given}{Find}{SI}{Solution}并输出类似这样的内容:

在此处输入图片描述

选修的

  1. Given和部分应具有恒定的宽度FindSolution并根据其中的数据需要向下拉伸。
  2. SI部分应具有较小的最大宽度(类似于 7 个普通字符),如果其中的行较小,则缩小,并向下拉伸至其所需的数据。

例子

\PhysicsProblem{$x=2$\newline$y=3$}{$x + y$ - ?}{}{$x + y = 2 + 3 = 5$}

应该吐出这个:

在此处输入图片描述

您能创建一个这样的函数吗?谢谢!

答案1

你说:

  1. SI 部分应该有一个较小的最大宽度(比如 7 个正常字符),并且延伸到左边(如果数据引脚足够小)并下载所需的数据。

不清楚你所说的“延伸到左边“。

如果这意味着 SI 列应左不齐/右齐,则以下没有任何表格环境的方法可能会引起人们的兴趣:

\documentclass{article}
\usepackage{amsmath, textcomp, microtype}
\hyphenation{stret-ch-es}

%-----<begin of code for \PhysicsProblem>---------------------------------
%
% The code for \PhysicsProblem does not require additional packages.
%
% The packages above are loaded only for prettifying this example.
%
\newcommand\PhysicsProblemSepAboveFirstRow{\fboxsep}
\newcommand\PhysicsProblemSepAboveHorizontalRule{\fboxsep}
\newcommand\PhysicsProblemSepBelowHorizontalRule{\fboxsep}
\newcommand\PhysicsProblemSepBelowSecondRow{\fboxsep}
\newcommand\PhysicsProblemColSep{\tabcolsep}
\newcommand\PhysicsProblemWidthOfFirstColumn{3cm}%
\newcommand\PhysicsProblemWidthOfSecondColumn{5.9em}% 7 characters m
\newcommand\PhysicsProblemWidthOfThirdColumn{4cm}%
%
% (La)TeX's default line-thickness is 0.4pt
%
% Width of \PhysicsProblem =
%   0.4pt + \PhysicsProblemColSep + \PhysicsProblemWidthOfFirstColumn  + \PhysicsProblemColSep + 0.4pt
%         + \PhysicsProblemColSep + \PhysicsProblemWidthOfSecondColumn + \PhysicsProblemColSep + 0.4pt
%         + \PhysicsProblemColSep + \PhysicsProblemWidthOfThirdColumn  + \PhysicsProblemColSep + 0.4pt
%
% If you wish you can specify values so that
%    Width of \PhysicsProblem = \textwidth
% or
%    Width of \PhysicsProblem = \textwidth - \parindent
%
\newcommand\PhysicsProblem[4]{%
  \leavevmode
  \vtop{%
  \vbox{%
    \hrule %------Line at the top
    \hbox{%
      \vrule %-------Line at the left
      \vtop{%
        \hbox{%
          \kern\PhysicsProblemColSep
          \vbox{%
            \kern\PhysicsProblemSepAboveFirstRow\relax
            \hbox{\parbox[t]{\PhysicsProblemWidthOfFirstColumn}{\leavevmode#1}}%
          }%
          \kern\PhysicsProblemColSep
        }%
        \kern\PhysicsProblemSepAboveHorizontalRule\relax
        \hrule
        \kern\PhysicsProblemSepBelowHorizontalRule\relax
        \hbox{%
          \kern\PhysicsProblemColSep
          \vbox{\hbox{\parbox[t]{\PhysicsProblemWidthOfFirstColumn}{\leavevmode#2}}}%
          \kern\PhysicsProblemColSep
        }%
        \kern\PhysicsProblemSepBelowSecondRow\relax
      }%
      \vrule
      \kern\PhysicsProblemColSep
      \vtop{%
        \vbox{%
          \kern\PhysicsProblemSepAboveFirstRow\relax
          \hbox{\parbox[t]{\PhysicsProblemWidthOfSecondColumn}{%
            \leavevmode
            \raggedleft
            % When using package ragged2e:
            % \hyphenpenalty=50 %
            % \exhyphenpenalty=50 %
            % \finalhyphendemerits=0 %
            #3%
          }}%
        }%
        \kern\PhysicsProblemSepBelowSecondRow\relax
      }%
      \kern\PhysicsProblemColSep
      \vrule
      \kern\PhysicsProblemColSep
      \vtop{%
        \vbox{%
          \kern\PhysicsProblemSepAboveFirstRow\relax
          \hbox{\parbox[t]{\PhysicsProblemWidthOfThirdColumn}{\leavevmode#4}}%
        }%
        \kern\PhysicsProblemSepBelowSecondRow\relax
      }%
      \kern\PhysicsProblemColSep %------Horizontal distance between text and line at the right
      \vrule %------Line at the right
    }%
  }%
  \hrule %------Line at the bottom
  }%
}%
%
%-----<end of code for \PhysicsProblem>-----------------------------------

\pagestyle{empty}

\begin{document}
\enlargethispage{5cm}%
\vspace*{-3.75cm}%
\topsep=0ex
\partopsep=0ex

\noindent\par\hrule height 1.6pt\relax

\smallskip\noindent\textbf{\underline{Input:}}
\vfill

\begin{verbatim}
\PhysicsProblem{\textsf{\textsl{\textlangle Giveng\textrangle}}}%
               {\textsf{\textsl{\textlangle Find\textrangle}}}%
               {\textsf{\textsl{\textlangle SI\textrangle}}}%
               {\textsf{\textsl{\textlangle Solution\textrangle}}}%
\end{verbatim}

\vfill
\noindent\textbf{\underline{Result:}}
\vfill

\PhysicsProblem{\textsf{\textsl{\textlangle Given\textrangle}}}%
               {\textsf{\textsl{\textlangle Find\textrangle}}}%
               {\textsf{\textsl{\textlangle SI\textrangle}}}%
               {\textsf{\textsl{\textlangle Solution\textrangle}}}%

\vfill
\noindent\par\hrule height 1.6pt\relax

\smallskip\noindent\textbf{\underline{Input:}}
\vfill

\begin{verbatim}
\noindent Text
\PhysicsProblem{$x=2$\newline$y=3$}%
               {$x + y - \text{?}$}%
               {$\longleftarrow$ Some text for SI that stretches
                to the left.
                \hbox{mmmmmmm} 
                \hbox to\hsize{\null\hfill1\hfill\hfill2\hfill
                               \hfill3\hfill\hfill4\hfill\hfill
                               5\hfill\hfill6\hfill\hfill7\hfill
                               \null}
               }%
               {$x + y = 2 + 3 = 5$} Text 
\end{verbatim}

\vfill
\noindent\textbf{\underline{Result:}}
\vfill

\noindent Text
\PhysicsProblem{$x=2$\newline$y=3$}%
               {$x + y - \text{?}$}%
               {$\longleftarrow$ Some text for SI that stretches
                to the left.
                \hbox{mmmmmmm}
                \hbox to\hsize{\null\hfill1\hfill\hfill2\hfill
                               \hfill3\hfill\hfill4\hfill\hfill
                               5\hfill\hfill6\hfill\hfill7\hfill
                               \null}
               }%
               {$x + y = 2 + 3 = 5$} Text 

\vfill
\noindent\par\hrule height 1.6pt\relax

\smallskip\noindent\textbf{\underline{Input:}}
\vfill

\begin{verbatim}
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
ggggg

\PhysicsProblem{$x=2$\newline$y=3$}%
               {$x + y - \text{?}$}%
               {}%
               {$x + y = 2 + 3 = 5$}

Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
\end{verbatim}

\vfill
\noindent\textbf{\underline{Result:}}
\vfill

Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
ggggg

\PhysicsProblem{$x=2$\newline$y=3$}%
               {$x + y - \text{?}$}%
               {}%
               {$x + y = 2 + 3 = 5$}

Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text

\vfill
\noindent\par\hrule height 1.6pt\relax

\end{document}

在此处输入图片描述


另一种方法可能是嵌套表格环境。

使用以下示例命令

  • \PhysicsProblem\PhysicsProblemFramed生成如下表格:

    第二列的宽度:
    文本将被放入一个框中。该框内的每一行文本将向右对齐。该框中的行将被换行,以不超过宏中定义的宽度\PhysicsProblemMaximumWidthOfSecondColumn。然后,该框的宽度将尽可能减小,而不会改变换行符。

    第一列的宽度来自宏\PhysicsProblemWidthOfFirstColumn

    第三列的宽度来自宏\PhysicsProblemWidthOfThirdColumn

    在垂直线和文本列之间将根据长度留出水平空间\tabcolsep

    并且整个表格的宽度根据第二列的实际宽度而变化\PhysicsProblem\PhysicsProblemFramed

  • \PhysicsProblemX\PhysicsProblemXFramed生成如下表格:

    整个表格的宽度来自宏\PhysicsProblemXWidth

    第二列的宽度:
    文本将被放入一个框中。该框内的每一行文本将向右对齐。该框中的行将被换行,以不超过宏中定义的宽度\PhysicsProblemXMaximumWidthOfSecondColumn。然后,该框的宽度将尽可能减小,而不会改变换行符。

    然后计算表格的剩余宽度。(减去\PhysicsProblemXWidth该框的宽度和\tabcolsep垂直线的不同厚度。)

    \PhysicsProblemXProportionOfFirstColumn剩余的宽度被分成与宏和中存储的值的总和相对应的多个部分\PhysicsProblemXProportionOfThirdColumn

    第一列的宽度将由这些部分的数量组成,该数量与存储在宏中的值相对应\PhysicsProblemXProportionOfFirstColumn

    第三列的宽度将由这些部分的数量组成,该数量与存储在宏中的值相对应\PhysicsProblemXProportionOfThirdColumn

    并且整个表格的宽度不变。变化的是第一列和第三列的宽度取决于第二列的实际宽度\PhysicsProblemX\PhysicsProblemXFramed

 

\documentclass{article}
\hyphenation{stret-ch-es}

% Packages for prettifying this example:
\usepackage{amsmath, textcomp, microtype}

%---<begin of code for \PhysicsProblem/\PhysicsProblemFramde/etc>----------------------
% Packages needed by \PhysicsProblem/\PhysicsProblemFramde>/etc: eTeX-extensions and:
\usepackage{array, tabularx, varwidth, ragged2e}

\newcommand\PhysicsProblemWidthOfFirstColumn{4cm}%
\newcommand\PhysicsProblemMaximumWidthOfSecondColumn{35pt}%
\newcommand\PhysicsProblemWidthOfThirdColumn{3.5cm}%

\newcommand\PhysicsProblemXWidth{.8\textwidth}%
\newcommand\PhysicsProblemXMaximumWidthOfSecondColumn{35pt}% 7 characters m
\newcommand\PhysicsProblemXProportionOfFirstColumn{2}%
\newcommand\PhysicsProblemXProportionOfSecondColumn{4}%

% Column-type for the SI-column:
\newcolumntype{P}[1]{%
  >{%
     \begin{varwidth}[t]{#1}%
     \RaggedLeft
     %\hyphenpenalty=50 %
     %\exhyphenpenalty=50 %
     %\finalhyphendemerits=0 %
     \arraybackslash
  }%
  l%
  <{\end{varwidth}}%
}%

\newcommand\PhysicsProblem[4]{%
  \begin{tabular}[b]{@{}l@{}}%
    \begin{tabular}[t]{%
       @{}p{\dimexpr\PhysicsProblemWidthOfFirstColumn+\tabcolsep\relax}@{}%
      |P{\PhysicsProblemMaximumWidthOfSecondColumn}%
      |p{\PhysicsProblemWidthOfThirdColumn}@{}%
    }%
      \begin{tabular}[t]{@{}p{\dimexpr\hsize-\tabcolsep\relax}}%
        #1\\\hline#2%
      \end{tabular}%
      &#3\unskip\strut&#4\unskip\strut\\%
    \end{tabular}%
  \end{tabular}%
}%

\newcommand\PhysicsProblemFramed[4]{%
  \begin{tabular}[b]{@{}l@{}}%
    \hline
    \begin{tabular}[t]{%
      |@{}p{\dimexpr\PhysicsProblemWidthOfFirstColumn+2\tabcolsep\relax}@{}%
      |P{\PhysicsProblemMaximumWidthOfSecondColumn}%
      |p{\PhysicsProblemWidthOfThirdColumn}|%
    }%
      \begin{tabular}[t]{p{\dimexpr\hsize-2\tabcolsep\relax}}%
        #1\\\hline#2%
      \end{tabular}%
      &#3\unskip\strut&#4\unskip\strut\\%
      \hline
    \end{tabular}%
  \end{tabular}%
}%

\newcommand\PhysicsProblemX[4]{%
  \begin{tabular}[b]{@{}l@{}}%
    \begin{tabularx}{\PhysicsProblemXWidth}[t]{%
       @{}>{%
         \hsize=\dimexpr(%
                          (\hsize*(\number\numexpr\PhysicsProblemXProportionOfFirstColumn*2\relax))%
                          /%
                          (\number\numexpr\PhysicsProblemXProportionOfFirstColumn+%
                                          \PhysicsProblemXProportionOfSecondColumn\relax)%
                        )+\tabcolsep\relax
         \linewidth=\hsize
       }X@{}%
      |P{\PhysicsProblemXMaximumWidthOfSecondColumn}%
      |>{%
            \hsize=\dimexpr(\hsize*(\number\numexpr\PhysicsProblemXProportionOfSecondColumn*2\relax))%
                           /%
                           (\number\numexpr\PhysicsProblemXProportionOfFirstColumn+%
                                           \PhysicsProblemXProportionOfSecondColumn\relax)%
                           \relax
            \linewidth=\hsize
        }X@{}%
    }%
      \begin{tabular}[t]{@{}p{\dimexpr\hsize-\tabcolsep\relax}}%
        #1\\\hline#2%
      \end{tabular}%
      &#3\unskip\strut&#4\unskip\strut\\%
    \end{tabularx}%
  \end{tabular}%
}%

\newcommand\PhysicsProblemXFramed[4]{%
  \begin{tabular}[b]{@{}l@{}}%
    \hline
    \begin{tabularx}{\PhysicsProblemXWidth}[t]{%
      |@{}>{%
        \hsize=\dimexpr(%
                         (\hsize*(\number\numexpr\PhysicsProblemXProportionOfFirstColumn*2\relax))%
                         /%
                         (\number\numexpr\PhysicsProblemXProportionOfFirstColumn+%
                                         \PhysicsProblemXProportionOfSecondColumn\relax)%
                       )+2\tabcolsep\relax
        \linewidth=\hsize
      }X@{}%
      |P{\PhysicsProblemXMaximumWidthOfSecondColumn}%
      |>{%
         \hsize=\dimexpr(\hsize*(\number\numexpr\PhysicsProblemXProportionOfSecondColumn*2\relax))%
                        /%
                        (\number\numexpr\PhysicsProblemXProportionOfFirstColumn+%
                                        \PhysicsProblemXProportionOfSecondColumn\relax)%
                        \relax
         \linewidth=\hsize
       }X|%
    }%
      \begin{tabular}[t]{p{\dimexpr\hsize-2\tabcolsep\relax}}%
        #1\\\hline#2%
      \end{tabular}%
      &#3\unskip\strut&#4\unskip\strut\\%
      \hline
    \end{tabularx}%
  \end{tabular}%
}%
%---<end of code for \PhysicsProblem/\PhysicsProblemFramed/etc>------------------------

\begin{document}

With these examples \verb|\PhysicsProblemMaximumWidthOfSecondColumn| and
\verb|\PhysicsProblemXMaximumWidthOfSecondColumn| are defined to 35pt.

That corresponds to the length of the following rule:\hfill 
\hbox to 40pt{\hfill\rule{35pt}{.4pt}\hfill}

For comparison seven digits: \hfill \hbox to 40pt{\hfill\hbox{1234567}\hfill}

Each line of text within the second column will be flushed to the right.
The text of the second column will be placed into a box and hereby will be
wrapped to fit into lines of a width of 35pt. Then the width of that
box will be reduced as much as possible without changing the line-breaks.

\bigskip


Problem: \PhysicsProblem{$x=2$\newline$y=3$}%
           {$x + y - \text{?}$}%
           {Some text for SI that stretches to the left.}%
           {$x + y = 2 + 3 = 5$}

\bigskip

Problem: \PhysicsProblemFramed{$x=2$\newline$y=3$}%
           {$x + y - \text{?}$}%
           {Some text for SI that stretches to the left.}%
           {$x + y = 2 + 3 = 5$}

\bigskip

Problem: \PhysicsProblemX{$x=2$\newline$y=3$}%
           {$x + y - \text{?}$}%
           {Some text for SI that stretches to the left.}%
           {$x + y = 2 + 3 = 5$}

\bigskip

Problem: \PhysicsProblemXFramed{$x=2$\newline$y=3$}%
           {$x + y - \text{?}$}%
           {Some text for SI that stretches to the left.}%
           {$x + y = 2 + 3 = 5$}

\end{document}

在此处输入图片描述

相关内容