我有以下代码来生成矩阵:
\begin{gather}
\begin{matrix}
\textcolor{blue}{LKC_1} \\
\textcolor{blue}{LKC_2} &
\end{matrix}
\putunder{
\begin{bmatrix}
\frac{1}{R_1} & -\frac{1}{R_1}\\
-\frac{1}{R_1} & \frac{1}{R_1}+\frac{1}{R_2}
\end{bmatrix}
}
{
\begin{matrix}
\textcolor{red}{Y}
\end{matrix}
}
\end{gather}
我有以下代码来生成表格:
\begin{table}[!h]
\normalsize
\caption{Elements [...].}
\label{tab:anm_estampas}
\begin{center}
%\setlength{\tabcolsep}{8pt}
\begin{tabular}{l | c | l | c }
\hline Elements & Symbol & Matricex & Equantions \\\hline
Resistor & & & \\
\end{tabular}
\end{center}
\fonte{Figuras do Autor}
\end{table}
蓝色箭头在哪里?我想放置一个宏来插入矩阵。我想这样做,以免表格中的大量代码造成污染。
因此,为了做到这一点,我捕获了生成矩阵的所有代码并将其放入如下宏中:
\newcommand{\matrixresistor}{%
\begin{gather}
\begin{matrix}
\textcolor{blue}{LKC_1} \\
\textcolor{blue}{LKC_2} &
\end{matrix}
\putunder{
\begin{bmatrix}
\frac{1}{R_1} & -\frac{1}{R_1}\\
-\frac{1}{R_1} & \frac{1}{R_1}+\frac{1}{R_2}
\end{bmatrix}
}
{
\begin{matrix}
\textcolor{red}{Y}
\end{matrix}
}
\end{gather}
}%
但这不管用。抛出错误:
我无法找出原因。提前致谢。
答案1
gather
正常细胞中不可能存在tabular
这种物质。我认为你用错了工具。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\[
\begin{bNiceMatrix}[first-col,last-row]
\textcolor{blue}{LKC_1} & \frac{1}{R_1} & -\frac{1}{R_1} \\[1.5ex]
\textcolor{blue}{LKC_2} & -\frac{1}{R_1} & \frac{1}{R_1}+\frac{1}{R_2} \\[1ex]
& \multicolumn{2}{c}{\mathcolor{red}{Y}}
\end{bNiceMatrix}
\]
\end{document}
我在数学显示中设置的对象可以在任何地方使用(在数学模式下)。
您可以像下面这样定义您的符号。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}
\newcommand{\matrixresistor}{%
\ensuremath{%
\begin{bNiceMatrix}[first-col,last-row]
\textcolor{blue}{LKC_1} & \frac{1}{R_1} & -\frac{1}{R_1} \\[1.5ex]
\textcolor{blue}{LKC_2} & -\frac{1}{R_1} & \frac{1}{R_1}+\frac{1}{R_2} \\[1ex]
& \multicolumn{2}{c}{\mathcolor{red}{Y}}
\end{bNiceMatrix}%
}%
}
\begin{document}
\begin{tabular}{l c l c }
\toprule
Elements & Symbol & Matrices & Equations \\
\midrule
Resistor & & \matrixresistor & \\
\end{tabular}
\end{document}
booktabs
建议使用命令,而不是使用标尺表。
答案2
关于 的定义\matrixresistor
:我建议您去掉align
包装器,改用\ensuremath
指令。这样,就可以使用宏,而无需事先知道它是在文本模式还是数学模式下使用。
\documentclass{article}
\usepackage{xcolor,amsmath,booktabs,threeparttable}
\newcommand*{\putunder}[2]{{\displaystyle\mathop{#1}_{\textstyle #2}}} % ?
\newcommand{\matrixresistor}{\ensuremath{%
\begin{matrix}
\textcolor{blue}{\mathit{LKC}_1} \\[1ex]
\textcolor{blue}{\mathit{LKC}_2}
\end{matrix}%
\putunder{%
\begin{bmatrix}
\frac{1}{R_1} & -\frac{1}{R_1} \\[1ex]
-\frac{1}{R_1} & \frac{1}{R_1} + \frac{1}{R_2}
\end{bmatrix}
}{ \textcolor{red}{Y} }
}}
\begin{document}
\begin{table}[ht]
\centering
\begin{threeparttable}
\caption{Elements [...].}
\label{tab:anm_estampas}
\begin{tabular}{@{} lccc @{}}
\toprule
Elements & Symbol & Matricex & Equantions \\
\midrule
Resistor & & \matrixresistor & \\
\bottomrule
\end{tabular}
\smallskip\footnotesize
Fonte: Figuras do Autor
\end{threeparttable}
\end{table}
\end{document}
答案3
\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\begin{document}
\def\matrixresistor
{$\begin{bNiceMatrix}[first-col,last-row]
\textcolor{blue}{LKC_1} & \frac{1}{R_1} & -\frac{1}{R_1} \\[1.5ex]
\textcolor{blue}{LKC_2} & -\frac{1}{R_1} & \frac{1}{R_1}+\frac{1}{R_2} \\[1ex]
& \multicolumn{2}{c}{\mathcolor{red}{Y}}
\end{bNiceMatrix}$}
\vspace{1cm}
\begin{tabular}{cccc}
\toprule
Elements & Symbol & Matricex & Equantions \\
\midrule
Resistor & & \matrixresistor \\
\bottomrule
\end{tabular}
\end{document}