希望节点根据哪个输入较大来具有链接高度

希望节点根据哪个输入较大来具有链接高度

因此,我正在尝试为学生制作一个工作表模板,以便创建供学生标记的 PDF。输入将创建问题框和提示框中的内容。

我想动态地创建这两个框的高度 - 也就是说我希望它们相同根据输入进行缩放。答案框应占据页面上的剩余空间。我遇到了一些问题:

  1. 我目前根据输入设置问题框的高度,并缩放提示框以匹配。我希望它们比输入框大一些。

  2. 我目前无法让生成的图像位于页面的中心。

我真的不确定我这样做是否最有效,但我愿意听取建议。我想使这个过程自动化,以便于我轻松地为学生制作内容。

提前感谢您付出的时间和精力。

    \documentclass{article}
    \pagestyle{empty}
    \usepackage[top=.5in, left=.5in, right=.5in, bottom=.5in]{geometry}
    \usepackage{tikz}
    \usepackage{calc}
    \usetikzlibrary{arrows, positioning, calc, fit}
    \newcommand\WS[2]{
    \begin{tikzpicture}

        % The Question
        \node[
            draw=red,
            fill=red!10, 
            rounded corners=6pt, 
            text width=.55\textwidth,
            minimum height=.1\textheight,
            inner sep=20pt,
            align=center] 
            (Question) 
            {
                #1
            }; 
        % The Label
        \node[
            draw=red,
            fill=red!30, 
            rounded corners=6pt, 
            anchor=north west, 
            inner sep=5pt] 
            at (Question.north west) 
            {
                Question
            };

        % The Hints
        \path let 
            \p1=($(Question.south)-(Question.north)$),
            \n1 = {veclen(\p1)-0.4pt}      % 0.4pt is the width of the border line
            in 
            node[
                draw=blue, 
                fill=blue!10, 
                rounded corners=6pt, 
                text width=.35\textwidth, 
                minimum height=\n1, 
                right=0pt of Question.north east, 
                anchor=north west,
                align=center, 
                inner sep=5pt]
                (Hint) 
                {
                    #2
                };
        % The Label
        \node[
            draw=blue,
            fill=blue!30, 
            rounded corners=6pt, 
            anchor=north west, 
            inner sep=5pt] 
            at (Hint.north west) 
            {
                Hint
            };

        % Space to Answer
        \path let 
            \p2 = ($(Question.west)-(Hint.east)$),
            \n2 = {veclen(\p2)-0.4pt},  % 0.4pt is the width of the border
                                        % line
            \p3 = ($(Question.north)-(Question.south)$),
            \n3 = {\textheight-veclen(\p3)-10.4pt} 
            in 
            node[
                draw=green, 
                fill=green!10, 
                rounded corners=6pt, 
                text height=\n3, 
                below=0pt of Question.south west,
                anchor=north west, 
                minimum width=\n2] 
                (Answer) 
                {
                };
        % The Label
        \node[
            draw=green,
            fill=green!30, 
            rounded corners=6pt, 
            anchor=north west, 
            inner sep=5pt] 
            at (Answer.north west) 
            {
                Answer
            };

    \end{tikzpicture}
    }

    \begin{document}

    \WS{
        Suppose you have a right triangle as presented below.  Let $a=2$ cm, and
        $b=3$ cm.  How long would $c$ be? 
        \par
        \begin{tikzpicture}
            \draw[rounded corners=0pt] (0,0) -- (2,0) -- (2,3) -- (0,0);
        \end{tikzpicture}
        \par
        Remember to leave your answers as a square root, and to show all work and
        leave units. 
        }
        {
        \begin{itemize}
            \item Think about the pythagorean formula.
            \item It involves squares.
            \item And adding.
        \end{itemize}
    }

    \end{document}

答案1

更新:改进了表格和标记的放置位置。极大地通过使用样式和循环提高了代码的可读性并减小了大小。


一个不同的想法。不要把所有东西都放在 tikz 节点中,而是把问题和提示放在一个表中。这应该可以制作出两个高度相同的“单元格”。然后\tikzmark在战略要点使用著名的宏,这样我们以后就可以在每个单元格周围画线并组成所需的布局。

以下 MWE 实现了这个想法:

\documentclass{article}
\pagestyle{empty}
\usepackage[top=.5in, left=.5in, right=.5in, bottom=.5in]{geometry}
\usepackage{tikz}
\usepackage{array}
\usetikzlibrary{calc}

% Declaration of tikzmark
\def\tikzmark#1{\tikz[remember picture, overlay]\coordinate (#1);}

% Some styles
\tikzset{
  Frame/.style={rounded corners = 6pt, fill opacity = .1, draw = #1, fill = #1},
  Label/.style={rounded corners = 6pt, anchor = north west, inner sep = 5pt, fill = #1!30, draw = #1},
}
\def\tablepadding{5mm}

\newcommand\WS[2]{%
% First, typeset the text inside two cells of a table
% and put several \tikzmarks at strategic points
% ------------------------------------------------------------------ table ----------
\tikzmark{table top}% This mark is at top of the centered environment which contains the table
\begin{center}
\tikzmark{table left}% This mark is at left border in the table
% The following contrived specification ensures a padding of \tablepadding around the cells
\begin{tabular}{@{\hskip\tablepadding}m{.54\textwidth}l@{}@{\hskip\tablepadding}m{.34\textwidth}@{\hskip\tablepadding}l@{}}
  \\[2mm]% Top separation
  #1 &\tikzmark{table column}& #2 & \\ % Table column marks the left edge of the Hint column
\end{tabular}\tikzmark{table right}% Mark at the right border of the table
\end{center}
\noindent\tikzmark{table bottom} % Mark the horizontal position of the bottom of the table
% -------------------------------------------------------------- end table ----------
% Then draw the frames and labels
\begin{tikzpicture}[remember picture, overlay] 
 % Compute the relevant corners
 \coordinate (Question-north west) at (table top-|table left);
 \coordinate (Question-south east) at (table bottom-|table column);
 \coordinate (Hints-north west)    at (table top-|table column);
 \coordinate (Hints-south east)    at (table bottom-|table right);
 \coordinate (Answer-north west)   at (table bottom-|table left);
 \coordinate (bottom margin) at ([yshift=.5in] current page.south);
 \coordinate (Answer-south east)   at (bottom margin-|table right);
 % Draw the three frames and labels in a single loop :-)
 \foreach \what/\color in {Question/red,Hints/blue,Answer/green} {
   \filldraw[Frame=\color] (\what-north west) rectangle (\what-south east);
   \node[Label=\color]  at (\what-north west) { \what };
  }
\end{tikzpicture}
}

\begin{document}
\noindent
\WS{
    Suppose you have a right triangle as presented below.  Let $a=2$ cm, and
    $b=3$ cm.  How long would $c$ be?
    \par
    \begin{center}
    \begin{tikzpicture}
      \draw (0,0) -- (2,0) node[midway,below] {$a$}
                  -- (2,3) node[midway,right] {$b$}
                  -- (0,0) node[midway,left]  {$c$};
    \end{tikzpicture}
    \end{center}
    \par
    Remember to leave your answers as a square root, and to show all work and
    leave units.
    }
    {
    \begin{itemize}
        \item Think about the pythagorean formula.
        \item It involves squares.
        \item And adding.
    \end{itemize}
}

得到期望的结果(编译后两次(根据remember picture选项要求):

结果

答案2

我有一个半方法:

解决方案 1

我们测量“问题”和“提示”框的高度,并使用 的最大高度text height。由于inner sep包含在测量高度中,因此我们再次减去 。

这有一个坏处:
我在 TikZ 中的节点内部遇到了问题,该节点\par位于框内。这可能是由 PlainTeX 导致的。我
没有使用\pars,而是将内部放置tikzpicturecenter环境中,在我看来,这看起来更好,因为它引入了垂直空间。

解决方案 1 ½

为了解决这个\par问题,我只将文本本身放入\parbox并测量其高度,类似于解决方案 1,但似乎还有一些其他需要调整的高度……

代码

\documentclass{article}
\pagestyle{empty}
\usepackage[top=.5in, left=.5in, right=.5in, bottom=.5in]{geometry}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{arrows, positioning, calc}
\tikzset{
    all nodes/.style={
        rounded corners=6pt,
    },
    small node/.style={
        all nodes,
        inner sep=5pt,
        anchor=north west,
    },
    big node/.style={
        all nodes,
        align=center,
    },
    question/.style={
        big node,
        draw=red,
        fill=red!10,
        text width=.55\textwidth,
        inner sep=20pt,
    },
    question label/.style={
        small node,
        draw=red,
        fill=red!30,
    },
    hint/.style={
        big node,
        draw=blue,
        fill=blue!10,
        text width=.35\textwidth,
        inner sep=5pt,
    },
    hint label/.style={
        small node,
        draw=blue,
        fill=blue!30,
    },
    answer/.style={
        big node,
        draw=green, 
        fill=green!10, 
    },
    answer label/.style={
        small node,
        draw=green,
        fill=green!30, 
    },
    reset/.style={
        rounded corners=0pt,
        minimum height=0pt,
        inner sep=.3333em,
        text width=,% reset
    }
}
\newlength{\nodeheighta}
\newlength{\nodeheightb}
\newcommand\WS[2]{%
  \sbox0{\tikz[inner sep=0pt,outer sep=0pt]{\node[question] (Question) {#1};}}%
  \sbox1{\tikz[inner sep=0pt,outer sep=0pt]{\node[hint] (Hint) {#2};}}%
  \setlength{\nodeheighta}{\ht0}%
  \setlength{\nodeheightb}{\ht1}%
  \ifdim\nodeheighta<\nodeheightb\nodeheighta=\nodeheightb\fi%
  \noindent%
    \begin{tikzpicture}
      \node[question, minimum height=\nodeheighta,] (Question) {#1}; 
      \node[question label] at (Question.north west) {Question};
      \node[hint, minimum height=\nodeheighta, right=0pt of Question.north east, anchor=north west] (Hint) {#2};
      \node[hint label] at (Hint.north west) {Hint};

      \path let 
        \p2 = ($(Question.west)-(Hint.east)$),
        \n2 = {veclen(\p2)-\pgflinewidth},  % 0.4pt is the width of the border line
        \p3 = ($(Question.north)-(Question.south)$),
        \n3 = {\textheight-veclen(\p3)-10.4pt} 
        in node[
          answer,
          text height=\n3, 
          below=0pt of Question.south west,
          anchor=north west, 
          minimum width=\n2
        ] (Answer) {};
      % The Label
      \node[answer label, anchor=north west, inner sep=5pt] at (Answer.north west) {Answer};
    \end{tikzpicture}%
}

\begin{document}
\WS{
  Suppose you have a right triangle as presented below.  Let $a=2$ cm, and
  $b=3$ cm.  How long would $c$ be? 
  \begin{center}
  \begin{tikzpicture}[reset,auto=right]
      \draw (0,0) -- node {$a$} (2,0) -- node {$b$} (2,3)  -- node {$c$} (0,0);
  \end{tikzpicture}
  \end{center}
  Remember to leave your answers as a square root, and to show all work and
  leave units.
  }
  {
  \begin{itemize}
      \item Think about the pythagorean formula.%\\~\\~\\~\\~\\~\\~\\~\\~\\~\\~\\~\\~\\~% dirty method to test when Hint > Question
      \item It involves squares.
      \item And adding.
  \end{itemize}
}
\end{document}

输出

在此处输入图片描述

答案3

这不是问题的答案,而是获得类似结果的另一种方法(我认为更容易)。

所提出的解决方案不再使用TikZ或多或少手动(calc+ )计算的节点,而是为我们完成所有工作。tikzmarktcolorbox

它使用一个tcbrasterwhere 选项raster equal height=rows将强制两个顶部框具有相同的高度,并且height fill第三个框上的选项将使其填满所有垂直可用空间。

\documentclass{article}
\usepackage{lmodern}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage[top=.5in, left=.5in, right=.5in, bottom=.5in]{geometry}

\tcbset{
    mybox/.style={
        enhanced,
        attach boxed title to top left={yshift*=-\tcboxedtitleheight},
        coltitle=black,
        colframe=#1,
        colback=#1!10,
        boxed title style={
            size=small,
            colback=#1!30},
        }
}

\newcommand{\WS}[2]{%
\begin{tcbitemize}[%
    raster force size=false,
    raster equal height=rows,
    raster row skip=0pt,
    raster column skip=0pt,
    raster row 1 column 1/.style={%
        mybox= red,
        add to width= .05\textwidth,
        title=Question},
    raster row 1 column 2/.style={%
        mybox= blue,
        add to width= -.05\textwidth,
        title=Hint},
    raster row 2 column 1/.style={%
        mybox= green,
        raster multicolumn=2, 
        height fill,
        title=Answer},  
    ]
\tcbitem #1 
\tcbitem #2
\tcbitem 
\end{tcbitemize}}

\pagestyle{empty}
\begin{document}
\WS{Suppose you have a right triangle as presented below.  Let $a=2$ cm, and $b=3$ cm.  How long would $c$ be? 
        {\par\centering
        \begin{tikzpicture}
            \draw[rounded corners=0pt] (0,0) -- (2,0) -- (2,3) -- (0,0);
        \end{tikzpicture}
        \par}
        Remember to leave your answers as a square root, and to show all work and leave units.}%
{  \begin{itemize}
      \item Think about the pythagorean formula.
      \item It involves squares.
      \item And adding.
  \end{itemize}}

\end{document}

在此处输入图片描述

相关内容