我正在一个框内输入是或否问题,但创建命令时遇到了困难。你介意帮我做点什么来输出这个吗:
答案1
正如我所说的定义一个命令(可能递归)以在命令中将文本换行 n 次,当谈到绘制盒子时,tikz
是正确的做法。
以下是一个宏版本,它接受每个文本元素的四个必需参数。因此
\BoxedQuestion{Is this what you are looking for?}{YES}{NO}{NONE}
你得到你的清楚的香草盒:
但是,它还接受两个可选参数,允许您设置外部和内部框的样式。这只是一个示例很少您可以调整各种选项,以便调整对齐、绘制和填充颜色:
盒子宽度:
要控制框的宽度,可以添加以下text width=<length>
选项:
盒子之间的水平间距:
已定义一个宏来指定水平间距。当前将其定义为\hfill
将三个框均匀分开。如果您确实希望它们之间有固定的间距,您可以调整它。因此,将其定义为
\newcommand*{\HorizontalSpacing}{\hspace*{2.0cm}}
你得到:
请注意,水平分离的固定距离需要小心,如上图第四个框所示。
此外,这需要在如何控制第一行的对齐方面采用略有不同的使用模式:而不是tikz
我\fill
在适当的一侧使用的选项。
代码:盒子均匀分布
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\newcommand*{\HorizontalSpacing}{\hfill}% equally spaced boxes
%\newcommand*{\HorizontalSpacing}{\hspace*{2.0cm}}% fixed space boxes
\newlength{\InnerSep}
\setlength{\InnerSep}{0.1cm}
\newcommand{\Boxed}[2][]{%
% #1 = box draw/fill options
% #2 = text
\begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
\node [#1] (Origin) {#2};
\draw [thick, draw=black, #1]
(Origin.south west) rectangle (Origin.north east) ;
\end{tikzpicture}%
}
\tikzset{Outer Box Style/.style={align=center}}
\tikzset{Inner Box Style/.style={
inner sep=2pt, yshift=0.7ex,
text width=1ex, minimum height=2ex
}
}
\newlength{\BoxWidth}
\NewDocumentCommand{\BoxedQuestion}{O{} O{} m m m m}{
% #1 = optional draw options for outer box
% #2 = optional draw options for inner boxes
% #3 = text of question
% #4 = choice 1
% #5 = choice 2
% #6 = choice 3
\setlength{\BoxWidth}{\dimexpr\linewidth-2\InnerSep-2\pgflinewidth\relax}%
\par\noindent\Boxed[Outer Box Style, text width=\BoxWidth, #1]{%
#3\strut%
\par\bigskip\noindent%
\hspace*{\InnerSep}\Boxed[Inner Box Style, #2]{}\,#4%
\HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#5%
\HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#6%
\hspace*{\InnerSep}\null%
}%
}%
\begin{document}
\BoxedQuestion
{Is this what you are looking for?}
{YES}{NO}{NONE}
\medskip
\BoxedQuestion[draw=red, align=left][draw=blue, fill=yellow!50]
{Is this what you are looking for?}
{YES}{NO}{NONE}
\medskip
\BoxedQuestion
[draw=red, ultra thick, loosely dotted, align=right,
fill=green!30, fill opacity=0.2, text opacity=1]
[solid, ultra thick, draw=blue, fill=orange!50]
{Is this what you are looking for?}
{YES}{NO}{NONE}
\medskip
\BoxedQuestion[text width=6cm]
[solid, ultra thick, draw=blue, fill=orange!50]
{Is this what you are looking for?}
{YES}{NO}{NONE}
\BoxedQuestion[text width=8cm]
[solid, ultra thick, draw=blue, fill=orange!50]
{Is this what you are looking for?}
{YES}{NO}{NONE}
\end{document}
代码:固定水平间距
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
%\newcommand*{\HorizontalSpacing}{\hfill}% equally spaced boxes
\newcommand*{\HorizontalSpacing}{\hspace*{2.0cm}}% fixed space boxes
\newlength{\InnerSep}
\setlength{\InnerSep}{0.1cm}
\newcommand{\Boxed}[2][]{%
% #1 = box draw/fill options
% #2 = text
\begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
\node [#1] (Origin) {#2};
\draw [thick, draw=black, #1]
(Origin.south west) rectangle (Origin.north east) ;
\end{tikzpicture}%
}
\tikzset{Outer Box Style/.style={align=center}}
\tikzset{Inner Box Style/.style={
inner sep=2pt, yshift=0.7ex,
text width=1ex, minimum height=2ex
}
}
\newlength{\BoxWidth}
\NewDocumentCommand{\BoxedQuestion}{O{} O{} m m m m}{
% #1 = optional draw options for outer box
% #2 = optional draw options for inner boxes
% #3 = text of question
% #4 = choice 1
% #5 = choice 2
% #6 = choice 3
\setlength{\BoxWidth}{\dimexpr\linewidth-2\InnerSep-2\pgflinewidth\relax}%
\par\noindent\Boxed[Outer Box Style, text width=\BoxWidth, #1]{%
#3\strut%
\par\bigskip\noindent%
\hspace*{\InnerSep}\Boxed[Inner Box Style, #2]{}\,#4%
\HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#5%
\HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#6%
\hspace*{\InnerSep}%
}%
}%
\begin{document}
\BoxedQuestion
{Is this what you are looking for?}
{YES}{NO}{NONE}
\medskip
\BoxedQuestion[draw=red][draw=blue, fill=yellow!50]
{Is this what you are looking for?\hfill }
{YES}{NO}{NONE}
\medskip
\BoxedQuestion
[draw=red, ultra thick, loosely dotted,
fill=green!30, fill opacity=0.2, text opacity=1]
[solid, ultra thick, draw=blue, fill=orange!50]
{\hfill Is this what you are looking for?}
{YES}{NO}{NONE}
\medskip
\BoxedQuestion[text width=6cm]
[solid, ultra thick, draw=blue, fill=orange!50]
{Is this what you are looking for?}
{YES}{NO}{NONE}
\BoxedQuestion[text width=8cm]
[solid, ultra thick, draw=blue, fill=orange!50]
{Is this what you are looking for?}
{YES}{NO}{NONE}
\end{document}
答案2
是的。盒子可以嵌套。
\documentclass{article}
\usepackage{calc}
\pagestyle{empty}
\begin{document}
\fbox{\begin{minipage}{\textwidth-2\fboxrule-2\fboxsep}
\centering
Is this what you're looking for?
\vspace{3ex}
\fbox{\rule{1em}{0pt}\rule{0pt}{1em}} YES \hspace*{\fill}
\fbox{\rule{1em}{0pt}\rule{0pt}{1em}} NO \hspace*{\fill}
\fbox{\rule{1em}{0pt}\rule{0pt}{1em}} NONE
\end{minipage}}
\end{document}