Skip to content

Commit 198ee16

Browse files
Added DataFrame>> #toLatex
1 parent 4f61df2 commit 198ee16

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,6 +5133,25 @@ DataFrameTest >> testToColumnsAtApplyElementwise [
51335133
self assert: df equals: expected
51345134
]
51355135

5136+
{ #category : #tests }
5137+
DataFrameTest >> testToLatex [
5138+
5139+
| expectedString |
5140+
expectedString := '\begin{tabular}{|l|l|l|l|}
5141+
\hline
5142+
\# & City & Population & BeenThere\\
5143+
\hline
5144+
''A'' & ''Barcelona'' & 1.609 & true \\
5145+
\hline
5146+
''B'' & ''Dubai'' & 2.789 & true \\
5147+
\hline
5148+
''C'' & ''London'' & 8.788 & false \\
5149+
\hline
5150+
\end{tabular}'.
5151+
5152+
self assert: df toLatex equals: expectedString
5153+
]
5154+
51365155
{ #category : #tests }
51375156
DataFrameTest >> testToMarkdown [
51385157

src/DataFrame/DataFrame.class.st

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,59 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [
22592259
self toColumnAt: each applyElementwise: aBlock ]
22602260
]
22612261

2262+
{ #category : #converting }
2263+
DataFrame >> toLatex [
2264+
" Prints the DataFrame as a Latex formatted table"
2265+
2266+
| markdown columnWidths dataFrame |
2267+
dataFrame := self copy.
2268+
dataFrame addColumn: dataFrame rowNames named: '\#' atPosition: 1.
2269+
markdown := WriteStream on: String new.
2270+
markdown nextPutAll: '\begin{tabular}{|'.
2271+
dataFrame numberOfColumns timesRepeat: [ markdown nextPutAll: 'l|' ].
2272+
markdown nextPutAll: '}'.
2273+
markdown cr.
2274+
markdown nextPutAll: '\hline'.
2275+
markdown cr.
2276+
2277+
columnWidths := dataFrame columnNames collect: [ :columnName |
2278+
| maxWidth |
2279+
maxWidth := columnName size.
2280+
dataFrame rows do: [ :row |
2281+
| value |
2282+
value := row at: columnName.
2283+
maxWidth := maxWidth max: value printString size ].
2284+
maxWidth ].
2285+
2286+
dataFrame columnNames withIndexDo: [ :columnName :index |
2287+
| paddedColumnName |
2288+
paddedColumnName := columnName padRightTo: (columnWidths at: index).
2289+
index = dataFrame numberOfColumns
2290+
ifFalse: [ markdown nextPutAll: paddedColumnName , ' & ' ]
2291+
ifTrue: [ markdown nextPutAll: paddedColumnName ] ].
2292+
markdown nextPutAll: '\\'.
2293+
markdown cr.
2294+
markdown nextPutAll: '\hline'.
2295+
markdown cr.
2296+
2297+
2298+
2299+
dataFrame asArrayOfRows do: [ :row |
2300+
row withIndexDo: [ :value :index |
2301+
| paddedValue |
2302+
paddedValue := value printString padRightTo:
2303+
(columnWidths at: index).
2304+
index = dataFrame numberOfColumns
2305+
ifFalse: [ markdown nextPutAll: paddedValue , ' & ' ]
2306+
ifTrue: [ markdown nextPutAll: paddedValue ] ].
2307+
markdown nextPutAll: '\\'.
2308+
markdown cr.
2309+
markdown nextPutAll: '\hline'.
2310+
markdown cr ].
2311+
markdown nextPutAll: '\end{tabular}'.
2312+
^ markdown contents
2313+
]
2314+
22622315
{ #category : #converting }
22632316
DataFrame >> toMarkdown [
22642317
" Prints the DataFrame as a Markdown formatted table"

0 commit comments

Comments
 (0)