
好吧,我一直在處理一些表格,對於數字對齊,我通常使用siunitx
嗯,自從 Mico 幫助我以來,這個問題。
但我在這些新表中遇到了一個我不明白如何解決的錯誤。
siunitx error: "invalid-number" Invalid numerical input 'e'.
For immediate help type H <return>. \end{tabularx}
我發現了這些關於它的問題:問題1和問題2。不幸的是,情況並非如此,我還沒有找到解決方案的線索。
我的 MWE 是:
\documentclass[fontsize=10pt,paper=letter,headings=small,bibliography=totoc,DIV=9,headsepline=true,titlepage=on]{scrartcl}
\usepackage[spanish,mexico]{babel}
\usepackage{xspace}
\usepackage{xkeyval}
\usepackage{array,multirow,multicol,rotating,tabularx,ragged2e,booktabs}
%\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
%\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\usepackage{rotating} % Paquete para rotar objetos flotantes
\usepackage{colortbl} % Paquete pata colorear tablas
\usepackage[per-mode=symbol]{siunitx} % Paquete para insertar unidades
\sisetup{
output-decimal-marker = {.},
group-minimum-digits = 4,
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a } % substitute the right word here
\ExplSyntaxOff
\begin{document}
\begin{table}[htbp]
\centering
\caption{Mercado de energía eléctrica en Norteamérica}
\label{tab:emna}
\begin{tabularx}{\linewidth}{@{}lYrYrYrYr @{}}
\toprule
País & Producción [\si{\giga\watt\hour}] & Fecha & Consumo [\si{\giga\watt\hour}] & Fecha & Exportaciones [\si{\giga\watt\hour}] & Fecha & Importaciones [\si{\giga\watt\hour}] & Fecha \\
\midrule
Canadá & 612000 & 2007 & 530000 & 2006 & 50120 & 2007 & 19660 & 2007 \\
Estados Unidos & 4167000 & 2007 & 3892000 & 2007 & 20140 & 2007 & 51400 & 2007 \\
México & 243300 & 2007 & 202000 & 2007 & 1278 & 2007 & 482.2 & 2007 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
我嘗試S
在中間列和右列中使用列類型,但我不能,因為前面提到的錯誤。我嘗試使用簡單的S
列但沒有成功,後來S[table-format=5.0]
但那不起作用。我的桌子出了什麼問題?
更新
儘管這兩個答案都非常有趣且有用,但恐怕我的問題仍然存在。我無法在表中新增列類型S
,但我需要它們。
現在我新增了一個目前處理相同問題的表,其中我Y
同時使用了列類型,但結果並不令人滿意。
我猜我的序言中的一個包是有責任的,看看我是否能檢測到它,因為 MWE 似乎工作順利。
答案1
(OP更改MWE中的表格後重寫答案。)
以下解決方案可讓您使用S
四個「GWh」列的列類型和讓您使用一個tabularx
環境(以確保表格的寬度等於\linewidth
)。這個技巧——就像它一樣——包括使用S
數字和C
(居中版本的X
)作為標題。
您會發現我已經重新組織了表格的標題。您的原始設定需要對所有四個重要的標題單字進行換行—— Producción
、Consumo
、Exportaciones
和Importaciones
。我認為最好(盡可能)避免這些單字的連字符。我在標題周圍留下了方括號GWh
;然而,它們可能並不需要。
(為了簡化和精簡前導碼程式碼,我還刪除了所有似乎對於生成表並不重要的套件。)
\documentclass[fontsize=10pt,paper=letter,headings=small,bibliography=totoc,
DIV=9,headsepline=true,titlepage=on]{scrartcl}
\usepackage[spanish,mexico]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of "X" column type
\newcommand\mc[1]{\multicolumn{2}{@{}C@{}}{#1}} % shortcut macro
\usepackage{siunitx} % Paquete para insertar unidades
\sisetup{
per-mode = symbol,
output-decimal-marker = {.},
group-minimum-digits = 4,
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a } % substitute the right word here
\ExplSyntaxOff
\begin{document}
\begin{table}
\caption{Mercado de energía eléctrica en Norteamérica}
\label{tab:emna}
\begin{tabularx}{\linewidth}{@{} l
*{2}{S[table-format=7.0]r}
S[table-format=5.0]r
S[table-format=5.1]r @{}}
\toprule
País & \mc{Producción} & \mc{Consumo} & \mc{Exportaciones} & \mc{Importaciones} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(l){8-9}
& [\si{\giga\watt\hour}] & Fecha & [\si{\giga\watt\hour}] & Fecha
& [\si{\giga\watt\hour}] & Fecha & [\si{\giga\watt\hour}] & Fecha \\
\midrule
Canadá & 612000 & 2007 & 530000 & 2006 & 50120 & 2007 & 19660 & 2007 \\
Estados Unidos & 4167000 & 2007 & 3892000 & 2007 & 20140 & 2007 & 51400 & 2007 \\
México & 243300 & 2007 & 202000 & 2007 & 1278 & 2007 & 482.2 & 2007 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
附錄:這是同一張表,但沒有重新組織標題材料。該程式碼與上面相同,只是Y
四個標題單元格使用了列類型。
....
\newcolumntype{Y}{>{\hspace{0pt}\RaggedRight\arraybackslash}X} % allow hyphenation
....
\begin{table}[htbp]
\setlength\tabcolsep{4pt}
\caption{Mercado de energía eléctrica en Norteamérica}
\label{tab:emna}
\begin{tabularx}{\linewidth}{@{}l
*{2}{S[table-format=7.0]r}
S[table-format=5.0]r
S[table-format=5.1]r @{}}
\toprule
País
& \multicolumn{1}{Y}{Producción [\si{\giga\watt\hour}]} & Fecha
& \multicolumn{1}{Y}{Consumo [\si{\giga\watt\hour}]} & Fecha
& \multicolumn{1}{Y}{Exportaciones [\si{\giga\watt\hour}]} & Fecha
& \multicolumn{1}{Y}{Importaciones [\si{\giga\watt\hour}]} & Fecha \\
\midrule
....
答案2
你需要的不是庫存tabularx
,而是庫存tabular*
。我只是將“Estados Unidos”縮寫為“EUA”,以便更好地適應可用空間。
請注意,列中的非數字輸入S
應加大括號;這樣,siunitx
就不會嘗試將文字解釋為數字,這就是「Exportaciones」儲存格中出現錯誤訊息的原因。
\documentclass[
fontsize=10pt,
paper=letter,
headings=small,
bibliography=totoc,
DIV=9,
headsepline=true,
titlepage=on
]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish,mexico]{babel}
\usepackage{booktabs}
\usepackage[per-mode=symbol]{siunitx} % Paquete para insertar unidades
\sisetup{
output-decimal-marker = {.},
group-minimum-digits = 4,
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}
\begin{document}
\begin{table}[htbp]
\centering
\caption{Mercado de energía eléctrica en Norteamérica}
\label{tab:emna}
\setlength{\tabcolsep}{1pt}% just a minimum
\begin{tabular*}{\linewidth}{
@{\extracolsep{\fill}}
l
S[table-format=7.0]
c
S[table-format=7.0]
c
S[table-format=5.0]
c
S[table-format=5.1]
c
@{}
}
\toprule
País & {Producción} & Fecha
& {Consumo} & Fecha
& {Exportaciones} & Fecha
& {Importaciones} & Fecha \\
& {(\si{\giga\watt\hour})} &
& {(\si{\giga\watt\hour})} &
& {(\si{\giga\watt\hour})} &
& {(\si{\giga\watt\hour})} & \\
\midrule
Canadá & 612000 & 2007 & 530000 & 2006 & 50120 & 2007 & 19660 & 2007 \\
EUA & 4167000 & 2007 & 3892000 & 2007 & 20140 & 2007 & 51400 & 2007 \\
México & 243300 & 2007 & 202000 & 2007 & 1278 & 2007 & 482.2 & 2007 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
答案3
這段程式碼有效。我藉此機會改進了您的表格:我認為您並不真正需要環境tabularx
,因此我Y
用普通的l
.當我認為有必要時,我還使用包將列頭製成兩行makecell
:
\documentclass[fontsize=10pt, paper=letter, headings=small, bibliography=totoc, DIV=9, headsepline=true, titlepage=on]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[spanish,mexico]{babel}
\usepackage{xspace}
\usepackage{xkeyval}
\usepackage{array,multirow,multicol,rotating,tabularx,ragged2e,booktabs}
\usepackage{ makecell}
\usepackage{rotating} % Paquete para rotar objetos flotantes
\usepackage{colortbl} % Paquete pata colorear tablas
\usepackage[per-mode=symbol]{siunitx} % Paquete para insertar unidades
\sisetup{
output-decimal-marker = {.},
group-minimum-digits = 4,
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a } % substitute the right word here
\ExplSyntaxOff
\begin{document}
\begin{table}[htb]
\centering
\caption{Reservas y Recursos Prospectivos}
\label{tab:RecOtorgados}%{0.85\linewidth}
\begin{tabular}{@{}l@{\qquad}S[table-format=5.0]S[table-format=5.0] @{}}%
\toprule
Tipo/Área & {\thead{Recursos 2P \\{} [MMBPCE]}} & {\thead{Recursos Prospectivo\\{} [MMBPCE]}} \\
\midrule
Convencional & 20589 & 18222 \\
Aguas Someras & 11374 & 7472 \\
Sureste & 11238 & 7472 \\
Norte & 136 & \\
Terrestre & 8818 & 5913 \\
Sur & 4379 & 5371 \\
Chicontepec & 3556 & \\
Burgos & 425 & \\
Resto Norte & 459 & 542 \\
Aguas Profundas & 397 & 4837 \\
Perdido & & 3013 \\
Holok-Han & 397 & 1824 \\
No Convencional & & 5225 \\
\midrule
Total & 20589 & 23447 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案4
首先,為了回答您的問題(修復您的錯誤):您在S
列中使用了一些非數位儲存格。它應該讀作(或以)“e”開頭。只需將此單元格放入大括號中即可。
現在,一般來說,對於您的表格:不要tabularx
與數字資料一起使用。由於您的表格對於 來說太大\linewidth
,只需手動減小寬度即可。
這看起來像這樣:
% arara: pdflatex
\documentclass[fontsize=10pt,paper=letter,headings=small,DIV=9,headsepline=true]{scrartcl}
\usepackage[spanish,mexico]{babel}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption}
%\usepackage{rotating} % do not load that twice
\usepackage{siunitx}
\sisetup{group-minimum-digits = 4}
\begin{document}
\begin{table}[htbp]
\centering
\tabcolsep=1.33ex
\caption{Mercado de energía eléctrica en Norteamérica}
\label{tab:emna}
\begin{tabular}{@{}lS[table-format=7.0]cS[table-format=7.0]cS[table-format=5.0]cS[table-format=5.1]c@{}}
\toprule
País & {Producción} & Fecha & {Consumo} & Fecha & {Exptciones} & Fecha & {Imptciones} & Fecha \\
& {en \si{\giga\watt\hour}} & & {en \si{\giga\watt\hour}} & & {en \si{\giga\watt\hour}} & & {en \si{\giga\watt\hour}} & \\
\midrule
Canadá & 612000 & 2007 & 530000 & 2006 & 50120 & 2007 & 19660 & 2007 \\
EEUU & 4167000 & 2007 & 3892000 & 2007 & 20140 & 2007 & 51400 & 2007 \\
México & 243300 & 2007 & 202000 & 2007 & 1278 & 2007 & 482.2 & 2007 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
如果您確實想使用tabularx
,您可以在這裡找到答案:如何一起使用siunitx和tabularx?
請勿在設備周圍使用支架。那是錯的。