0

Right now I need (for a project) a function, that will look like \PhysicsProblem{Given}{Find}{SI}{Solution} and spit out something like this:

enter image description here

Optional:

  1. The Given , Find and Solution sections should have constant width and stretch down as much as data in them needs.
  2. The SI section should have a small maximum width (something like 7 normal characters), shrink, if lines in it are smaller, and stretch down as much as data in it needs.

Example:

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

Should spit out this:

enter image description here

Could you, please, create a function like that? Thanks!

  • 7
    Welcome to TeX.SX! This is not a "do it for me" kind of site. If you want someone to be able to help you, at least provide a snippet of what you have tried and showing what was the limiting factor for you. – KersouMan Nov 21 '19 at 15:25
  • 1
    You want it empty like in the picture, or should it be filled with something? If it is filled with something, could you give us an example of it? – Alain Remillard Nov 21 '19 at 16:16
  • @AlainRemillard All those sections should be filled with corresponding arguments – MrWave Nov 21 '19 at 16:42
  • @MrWave Could you provide a filled example. An empty one is merely a tabular with p{length} column type. A filled one will be harder to do. Also, provide a minimal example with what you have tried. – Alain Remillard Nov 21 '19 at 16:46
  • Any code you have tried so far? – Cragfelt Nov 21 '19 at 17:10
3

You say:

  1. The SI section should have a small maximum width (something like 7 normal characters) and stretch both to the left (if pines of data are small enough) and down as much as data in it needs.

It is not clear what you mean by "stretch to the left".

In case this means that the SI-column shall be ragged-left/flush-right, the following approach without any tabular-environments might be of interest:

\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}

enter image description here


Another approach could be nesting tabular-environments.

With the following example the commands

  • \PhysicsProblem and \PhysicsProblemFramed produce tables as follows:

    The width of the second column:
    The text will be placed into a box. Each line of text within that box will be flushed to the right. Lines in that box will be wrapped to be not longer than the width defined in the macro \PhysicsProblemMaximumWidthOfSecondColumn. Then the width of that box will be reduced as much as possible without changing the line-breaks.

    The width of the first column comes from the macro \PhysicsProblemWidthOfFirstColumn.

    The width of the third column comes from the macro \PhysicsProblemWidthOfThirdColumn.

    Between the vertical lines and the columns of text you will have horizontal space according to the length \tabcolsep.

    With \PhysicsProblem and \PhysicsProblemFramed the width of the entire table varies depending on the actual width of the second column.

  • \PhysicsProblemX and \PhysicsProblemXFramed produce tables as follows:

    The width of the entire table comes from the macro \PhysicsProblemXWidth.

    The width of the second column:
    The text will be placed into a box. Each line of text within that box will be flushed to the right. Lines in that box will be wrapped to be not longer than the width defined in the macro \PhysicsProblemXMaximumWidthOfSecondColumn. Then the width of that box will be reduced as much as possible without changing the line-breaks.

    Then the remaining width of the table is calculated. (From \PhysicsProblemXWidth subtract the width of that box and diverse \tabcolsep and diverse thicknesses of vertical rules.)

    The remaining width is divided into an amount of parts that corrsponds to the sum of the values stored in the macros \PhysicsProblemXProportionOfFirstColumn and \PhysicsProblemXProportionOfThirdColumn.

    The width of the first column will be formed by an amount of these parts that corresponds the value stored in the macro \PhysicsProblemXProportionOfFirstColumn.

    The width of the third column will be formed by an amount of these parts that corresponds the value stored in the macro \PhysicsProblemXProportionOfThirdColumn.

    With \PhysicsProblemX and \PhysicsProblemXFramed the width of the entire table does not vary. What varies is the width of the first and third column depending on the actual width of the second column.

 

\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}

enter image description here

  • By left I meant that the width of the column can become smaller, if the lines in it are small enough, but still thanks a lot, I really appreciate that. – MrWave Nov 26 '19 at 15:18
  • @MrWave So the text in the lines is not to be raggedleft/flushright? In which way shall the lines in the SI-column become smaller? By now with my second example despite being \raggedleft, which can easily be changed, linebreaks are done for lines to fit the given maximum-width and then lines will be made as narrow as possible without changing line-breaks. Other things might be possible, depending on how much the SI-column may stick towards the bottom of the page.Extrem case of a small width would be: Allow a lot of hyphenation and have each syllable of the text on a line of its own... – Ulrich Diez Nov 26 '19 at 23:30
  • For example, if nothing passed in the SI parameter (look at the Example in my question), there should be two vertical lines really close to eachother (SI should shrink) – MrWave Nov 29 '19 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.