返回实用的文本框 frame在使用 ConTeXt 的过程中,会发现很多命令参数都显示inherits: \setupframed。在我自己使用 ConTeXt 的过程中,\framed也被高频使用。frame用的好,可以灵活扩展很多命令行的效果。
\framed 和\inframed
有一条和 frame 非常相似的命令\inframed,可以看到\framed命令和\inframed命令参数一致,但在pdf结果中,\framed的下边框线和行内文字对齐,\inframed内的文字基线和行内文字基线对齐。
一句话展示\framed{framed}和\inframed{inframed}的区别
framed 盒子的常用参数
*为了对比不同的参数效果,本文内先使用\setupframed设置盒子的统一样式,再在使用时通过\framed[参数]{文字}来设置每个盒子的特征点。
Frame & rulethickness
\framed的frame(也就是边框线)参数默认是开启状态,我们可以只开启部分边框,并设置边框粗细。
\setupframed[frame=off,width=4cm,height=3cm]
\hbox{
{\framed[leftframe=on,rulethickness=1pt]{\bf leftframe\\1pt}} \quad
{\framed[topframe=on,rulethickness=3pt]{\bf topframe\\3pt}} \quad
{\framed[rightframe=on,rulethickness=5pt]{\bf rightframe\\5pt}} \quad
{\framed[bottomframe=on,rulethickness=7pt]{\bf bottomframe\\7pt}} \quad
}
framecolor
framecolor 改变边框颜色
\setupframed[frame=on,rulethickness=2pt,width=4cm,height=3cm]
\hbox{
{\framed[framecolor=green]{\bf framecolor\\green}} \quad
{\framed[framecolor=red]{\bf framecolor\\red}} \quad
{\framed[framecolor=blue]{\bf framecolor\\blue}} \quad
{\framed[framecolor=orange]{\bf framecolor\\orange}} \quad
}
foregroundcolor
可以使用 foregroundcolor 改变框内文字的颜色
\setupframed[frame=on,rulethickness=2pt,width=4cm,height=3cm]
\hbox{
{\framed[foregroundcolor=green]{\bf foregroundcolor\\green}} \quad
{\framed[foregroundcolor=red]{\bf foregroundcolor\\red}} \quad
{\framed[foregroundcolor=blue]{\bf foregroundcolor\\blue}} \quad
{\framed[foregroundcolor=orange]{\bf foregroundcolor\\orange}} \quad
}
backgroundcolor
backgroundcolor 改变盒子的背景颜色
\setupframed[frame=on,rulethickness=2pt,width=4cm,height=3cm,background=color]
\hbox{
{\framed[backgroundcolor=green,foregroundcolor=white]{\bf foregroundcolor\\green}} \quad
{\framed[backgroundcolor=red,foregroundcolor=white]{\bf foregroundcolor\\red}} \quad
{\framed[backgroundcolor=blue,foregroundcolor=white]{\bf foregroundcolor\\blue}} \quad
{\framed[backgroundcolor=orange,foregroundcolor=white]{\bf foregroundcolor\\orange}} \quad
}
Offset
offset 可以调整盒子内文字和边框的距离, 下面我们可以对四条边的每条边框分别设置 2 em 来直观的看下 offset 的效果,当然也可以直接使用 offset = 2em 来对四条边设置相同的距离。代码见Gitee - frame_6.tex。
location
location 可以设置盒子基于行内文字基线的位置。代码见Gitee - frame_7.tex。
align
align 可以设置盒子内的文本对齐方式。其中需要注意下 right 和 left 的效果,right 最初代表的是 "ragged right", left 最初代表 "ragged left", 所以 right 不代表直译的右对齐,而代表左对齐。这是 ConTeXt 的历史问题,而修正这个名字会导致之前不同版本的 ConTeXt 效果不同,便延续到现在。如果怕混淆的话,可以使用 flushright 和 flushleft。代码见Gitee - frame_8.tex。
align 可以多个选项结合使用
align 里面的 top,lohi,low 等和 location 里面的 top,lohi,low 有什么区别?
location 强调整个盒子基于行内基线的位置,align 强调盒子内的文字在盒子里的垂直位置。
当相同样式的盒子需要多次使用时,可以使用\defineframed定义一个新盒子的名字并配置属性,然后使用新名字作为盒子命令
比如通过以下代码可以定义一个叫 myframe 的盒子,这个盒子带边框线,线宽 3 磅,盒子宽高 2 cm,背景颜色绿色。
\defineframed[myframe][frame=on,rulethickness=3pt,width=2cm,height=2cm,background=color,backgroundcolor=green]
定义好以后,可以直接通过 \myframe{} 命令使用(图一)。也可以在使用时另外添加参数来覆盖之前的属性或添加新属性(图二,取消边框线)。
\defineframed[myframe][frame=on,rulethickness=3pt,width=2cm,height=2cm,background=color,backgroundcolor=green]
\hbox{
{\myframe{ABC}} \quad
{\myframe[frame=off]{ABC}} \quad
}