我有以下代码:
\chemfig[][scale=0.6]{
CH_2=[:-30]CH-[:30]C(=[:90]O)-[:-30]O-[:30]CH_2CH_2-[:-30]O-[:30]C(=[:90]O)-[:-30]CH=[:30]CH_2
}
这给了我这样的图像:
现在我想在 O-CH_2CH_2 之间画开括号,在 OC 之间画闭括号。并用 n 对闭括号进行下标。我该如何实现?我必须使用tikzpicture
环境还是\leftdelimiter(
。我面临的问题是连接器的对齐和长度。
编辑
我有一些额外的图形,在这种情况下我该如何实现括号?
\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}
\def\makebraces(#1,#2)#3#4#5{%
\edef\delimhalfdim{\the\dimexpr(#1+#2)/2}%
\edef\delimvshift{\the\dimexpr(#1-#2)/2}%
\chemmove{
\node[at=(#4),yshift=(\delimvshift)]
{$
\left\delimleft
\vrule height\delimhalfdim depth\delimhalfdim width0pt
\right.
$};
\node[at=(#5),yshift=(\delimvshift)]
{$
\left.
\vrule height\delimhalfdim depth\delimhalfdim width0pt
\right\delimright_{\rlap{#3}}
$};
}%
}
\setpolymerdelim[]
\chemfig[][scale=0.6]{
?(-[2]CH_2-[@{op,.8}:180,3])(-[::-30,1.5]O-[::+60](-[:-30,1.5]O-[@{cl,.8}:0,3])<[::-150,1.5](-[:-50]O(-[7](=[6]O)-[1](-[7]CH_3)=[8]CH_2))-[::-90,1.5,,,line width=3pt](-[:45,1.5]OH)>[::+60]?(-[:-120]OH))
}
\makebraces(50pt,50pt){$\scriptstyle\!\!n$}{op}{cl}
图片如下:
答案1
一个例子是记录chemfig
(第三部分第 12.5 节绘制聚合物元素)有一个宏
\makebraces(<dim up>,<dim down>){<subscript>}{<opening node name>}{<closing node name>}
定义为利用公式内的chemfig
语法@{<node name>,<pos>}
来定位键上的分隔符。
下面的代码基本上是从那里复制的:
\documentclass{article}
\usepackage{chemfig}
\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}
\def\makebraces(#1,#2)#3#4#5{%
\edef\delimhalfdim{\the\dimexpr(#1+#2)/2}%
\edef\delimvshift{\the\dimexpr(#1-#2)/2}%
\chemmove{
\node[at=(#4),yshift=(\delimvshift)]
{$
\left\delimleft
\vrule height\delimhalfdim depth\delimhalfdim width0pt
\right.
$};
\node[at=(#5),yshift=(\delimvshift)]
{$
\left.
\vrule height\delimhalfdim depth\delimhalfdim width0pt
\right\delimright_{\rlap{#3}}
$};
}%
}
\begin{document}
\setpolymerdelim()
\chemfig{
CH_2=[:-30]CH-[:30]C(=[:90]O)-[:-30]O
-[@{op,.75}]CH_2CH_2-[@{cl,.25}]
O-[:30]C(=[:90]O)-[:-30]CH=[:30]CH_2
}
\makebraces(5pt,5pt){$\!\!n$}{op}{cl}
\end{document}
第一部分\makebraces(<dim up>,<dim down>)
指定括号的高度(分为向上和向下部分)。括号可以通过<pos>
的部分来定位。字体大小可以在的部分@{<node name>,<pos>}
中更改。当然,它也可以在宏本身中进行硬编码。<subscript>
\makebraces
\setpolymerdelim()
\chemfig{
-[@{op,.75}:30]CH_2CH_2-[:-30]O-[@{cl,.5}:30]
}
\makebraces(12pt,12pt){$\scriptstyle\!\!n$}{op}{cl}
为了更加灵活,可以改变的定义\makebraces
以获得每个括号不同的上下高度。
根据已编辑的问题进行编辑:
下面的代码是对该\makebraces
命令的快速修改,使参数(<dim up>,<dim down>)
可选,并将两者设置5pt
为两个分隔符的默认值。它还引入了第二个可选项(<dim up>,<dim down>)
,使用时会为结束分隔符设置不同的值。为了方便起见,这是借助xparse
. 这样可以更好地控制两个分隔符。
\documentclass{article}
\usepackage{chemfig}
\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}
\usepackage{xparse}
\makeatletter
\newcommand\@set@open@delim[2]{%
\edef\open@delim@halfdim{\the\dimexpr(#1+#2)/2\relax}%
\edef\open@delim@vshift{\the\dimexpr(#1-#2)/2\relax}%
}
\newcommand\@set@close@delim[2]{%
\edef\close@delim@halfdim{\the\dimexpr(#1+#2)/2\relax}%
\edef\close@delim@vshift{\the\dimexpr(#1-#2)/2\relax}%
}
\newcommand\@make@braces[3]{%
\chemmove{
\node[at=(#2),yshift=(\open@delim@vshift)]
{$
\left\delimleft
\vrule height\open@delim@halfdim depth\open@delim@halfdim width0pt
\right.
$};
\node[at=(#3),yshift=(\close@delim@vshift)]
{$
\left.
\vrule height\close@delim@halfdim depth\close@delim@halfdim width0pt
\right\delimright_{\rlap{#1}}
$};
}%
}
\NewDocumentCommand\makebraces
{
>{ \SplitArgument{1}{,} } D(){5pt,5pt}
>{ \SplitArgument{1}{,} } d()
mmm
}
{%
\@set@open@delim #1%
\IfNoValueTF {#2}
{\@set@close@delim#1}
{\@set@close@delim#2}%
\@make@braces{#3}{#4}{#5}%
}
\makeatother
\begin{document}
\setpolymerdelim[]
\chemfig[][scale=0.6]{
?(-[2]CH_2-[@{op,.8}:180,3])(-[::-30,1.5]O-[::+60](-[:-30,1.5]O-[@{cl,.8}:0,3])<[::-150,1.5](-[:-50]O(-[7](=[6]O)-[1](-[7]CH_3)=[8]CH_2))-[::-90,1.5,,,line width=3pt](-[:45,1.5]OH)>[::+60]?(-[:-120]OH))
}
\makebraces(10pt,100pt)(46pt,64pt){$\scriptstyle\!\!n$}{op}{cl}
\end{document}
答案2
在这种情况下,使用\rlap
和\llap
似乎更容易,因为不需要复制和调整复杂的宏:
\def\leftparen{\llap{$\left(\strut\right.$\kern5pt }}
\def\rightparen{\rlap{$\kern5pt \left.\strut\right)_n$}}
\chemfig{
CH_2=[:-30]CH-[:30]C(=[:90]O)-[:-30]O
-[:30]\leftparen CH_2CH_2|\rightparen
-[:-30]O-[:30]C(=[:90]O)-[:-30]CH=[:30]CH_2
}
这使
话虽如此,我不确定 OP 想要的括号是否正确……
编辑总是很复杂,克莱门斯...你可以做一些简单的事情,而不需要复杂的xparse
包:
\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}
\def\makebraces(#1,#2)#3#4#5{%
\edef\delimhalfdim{\the\dimexpr(#1+#2)/2}%
\edef\delimvshift{\the\dimexpr(#1-#2)/2}%
\chemmove{%
\node[at=(#4),yshift=(\delimvshift)]
{\rlap{$
\left\delimleft
\vrule height\delimhalfdim depth\delimhalfdim width0pt
\kern#5
\right\delimright_{#3}$}%
};%
}}
\setpolymerdelim[]
\chemfig[][scale=0.6]{
?(-[2]CH_2-[@{op,.8}:180,3])(-[::-30,1.5]O-[::+60](-[:-30,1.5]O-[:0,3])<[::-150,1.5](-[:-50]O(-[7](=[6]O)-[1](-[7]CH_3)=[8]CH_2))-[::-90,1.5,,,line width=3pt](-[:45,1.5]OH)>[::+60]?(-[:-120]OH))
}
\makebraces(5pt,90pt){n}{op}{130pt}
结果是