Computational Economics

computational tools for economists

Export all charts from a Calc spreadsheet to EPS_Macro

leave a comment »

Calc spreadsheet: Tool -> Macro -> Organize Dialogues… -> New -> copy and paste the macro you have

Code

Sub Main 
   Dim oDoc, oDocCtrl, oDocFrame, oDispatchHelper 
   oDoc = ThisComponent 
   oDocCtrl = oDoc.getCurrentController() 
   oDocFrame = oDocCtrl.getFrame() 
   oDispatchHelper = createUnoService( “com.sun.star.frame.DispatchHelper” ) 

   Dim storeUrl 
   storeUrl = oDoc.getURL() 
   storeUrl = Left( storeUrl, Len( storeUrl ) – 4 ) 

   nCharts = 0 

   ‘ Search the draw page for the chart. 
   Dim oSheets, oSheet, oDrawPage, oShape 
   oSheets = oDoc.getSheets() 
   For i = 0 to oSheets.getCount() – 1 
      oSheet = oSheets.getByIndex( i ) 
      oDrawPage = oSheet.getDrawPage() 
      For j = 0 to oDrawPage.getCount() – 1 
         oShape = oDrawPage.getByIndex( j ) 
         ‘ Can’t call supportsService unless the com.sun.star.lang.XServiceInfo is present. 
         If HasUnoInterfaces( oShape, “com.sun.star.lang.XServiceInfo” ) Then 
            If oShape.supportsService( “com.sun.star.drawing.OLE2Shape” ) Then 
               ‘ Is it a Chart? 
               If oShape.CLSID = “12DCAE26-281F-416F-a234-c3086127382e” Then 
                  ‘ Select the chart shape. 
                  oDocCtrl.select( oShape ) 
                  oDispatchHelper.executeDispatch( oDocFrame, “.uno:Copy”, “”, 0, Array() ) 
                  ‘ export the chart 
                  nCharts = nCharts + 1 
                  ExportSelection( storeUrl + “_chart” + nCharts + “.eps”, “image/x-eps” ) 
               EndIf 
            EndIf 
         EndIf 
      Next 
   Next 
End Sub 

Sub ExportSelection(url As String, mediaType As String) 
   ‘ Create a new Draw document 
   Dim aArgs(1) As New com.sun.star.beans.PropertyValue 
   aArgs(0).Name = “Hidden” 
   aArgs(0).Value = True 
   oDrawDoc = StarDesktop.loadComponentFromURL( “private:factory/sdraw”, “_blank”, 0, aArgs() ) 

   ‘ Past current selection 
   Dim oDrawDocCtrl, oDrawDocFrame, oDispatchHelper 
   oDrawDocCtrl = oDrawDoc.getCurrentController() 
   oDrawDocFrame = oDrawDocCtrl.getFrame() 
   oDispatchHelper = createUnoService( “com.sun.star.frame.DispatchHelper” ) 
   oDispatchHelper.executeDispatch( oDrawDocFrame, “.uno:Paste”, “”, 0, Array() ) 

   ‘ Get an export filter object 
   Dim exportFilter 
   exportFilter = createUnoService( “com.sun.star.drawing.GraphicExportFilter” ) 

   ‘ get first draw page 
   Dim oDrawPages, oDrawPage, oShape 
   oDrawPages = oDrawDoc.getDrawPages() 
   oDrawPage = oDrawPages.getByIndex( 0 ) 
   oShape = oDrawPage.getByIndex( 0 ) 
   exportFilter.setSourceDocument( oShape ) 

   ‘ Set the filter data 
   Dim aFilterData(5) As New com.sun.star.beans.PropertyValue 
   aFilterData(0).Name = “Level” ‘1=PS level 1, 2=PS level 2 
   aFilterData(0).Value = 2 
   aFilterData(1).Name = “ColorFormat” ‘1=color, 2=grayscale 
   aFilterData(1).Value = 1 
   aFilterData(2).Name = “TextMode” ‘0=glyph outlines, 1=no glyph outlines, see ooo bug 7918 
   aFilterData(2).Value = 1 
   aFilterData(3).Name = “Preview” ‘0=none, 1=TIFF, 2=EPSI, 3=TIFF+EPSI 
   aFilterData(3).Value = 0 
   aFilterData(4).Name = “CompressionMode” ‘1=LZW, 2=none 
   aFilterData(4).Value = 2 

   Dim aProps(2) As New com.sun.star.beans.PropertyValue 
   aProps(0).Name = “MediaType” 
   aProps(0).Value = mediaType 
   aProps(1).Name = “URL” 
   aProps(1).Value = url 
   aProps(2).Name = “FilterData” 
   aProps(2).Value = aFilterData() 

   exportFilter.filter( aProps() ) 
End Sub

/**************************************************************************
*
* Copyright 2007 Jose Fonseca
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* “Software”), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
**************************************************************************/

Advertisements

Written by Jingjing Wang

10/31/2011 at 12:21 pm

Posted in Calc

Add space between the lines in multiline formulas

leave a comment »

In LATEX additional line space is specified as optional argument of the new line command. This is not yet possible in LYX33, therefore the whole formula must be inserted in TEX-mode. To add in our example space, the command \\[3mm] is inserted at the end of the first line.

1. select the part you’re interested, View -> View Source, copy the TeX code

2. go to .lyx, Insert -> Tex code (Ctrl+L), paste the code into the redbox, and add \\[3mm] at the end of the first line

 

To set the line separation for all lines in a formula, the length \jot is changed.

1. load the LATEX-package calc in LATEX-preamble with the line

\usepackage{calc}

2. insert the command
\setlength{\jot}{3mm+3pt}

in TEX-mode before the formula

Written by Jingjing Wang

10/30/2011 at 7:50 pm

Posted in LyX

Break long formulas in a parenthesis in LyX

with one comment

Lyx has an “Insert -> Math -> AMS multiline environment” control, but does not work in the middle of a parenthesis.

How to break lines in the middle of a parenthesis?

In AMS multiline environment,

\left[\right.

(line break here) \left.\right]

Do not use Alt+M to get a parenthesis. Follow the way above. Close with ‘\right.’ before line-break and reopen with ‘\left.’

\left[\left[\right.\right.

(line break here) \left.\left.\right]\right]

This can lead to parentheses of different size. Size them yourself using ‘\big(‘, ‘\Big(‘, ‘\Bigg(‘, etc. or use \vphantom.

Written by Jingjing Wang

10/30/2011 at 6:40 pm

Posted in LyX

Pasting data into .lyx / Text wrapping in table in LyX

leave a comment »

Pasting data into .lyx

1. open your xls file

2. Select the requisite number of columns and rows

3. Come back to your lyx document

4. Insert a table which has at least the selected number of rows and columns

5. Go to the cell on your lyx table where you want the top left corner of the
section to be placed.

6. select either Edit -> Paste Special -> Plain Text or Edit -> Paste Special -> Plain Text, Join Lines

Text wrapping in table in LyX

LaTeX’s algorithms for formatting tables have a few shortcomings. One is that it will not automatically wrap text in cells, even if it overruns the width of the page. For columns that you know will contain a certain amount of text, then it is recommended that you use the p attribute and specify the desired width of the column (although it may take some trial-and-error to get the result you want). Use the m attribute to have the lines aligned toward the middle of the box and the b attribute to align along the bottom of the box.

LyX: right click in a cell -> more… -> setting… -> Table Settings (column settings, row settings, etc.)

Written by Jingjing Wang

10/29/2011 at 12:33 am

Posted in LyX

Generate EPS file for LaTeX/LyX and reduce the size if too big

leave a comment »

Reference

Wikibooks: LaTeX/Importing Graphics

CS professor at NCSU: Tools and Tips for Writing Papers

The only format you can include while compiling with latex is Encapsulated PostScript (EPS).

How to convert Microsoft Office Documents to EPS file?

Method 1

use the default printer in Windows 7: Metafile to EPS Converter

print –> current page/selection –> get the PRN file

use GSview (Ghostscript)

file –> PS to EPS –> get the EPS file with graph fitting to the bounding box

Method 2

use OpenOffice to open the file you have

file –> export –> get the EPS file

Conclusion

use Microsoft Office + Metafile to EPS Converter +  GSview

use OpenOffice directly

How to crop an image in EPS format?

use a text editor (e.g. Vim, Crimson) to open the file

The beginning of EPS usually looks like

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 555 735

(0,0) left bottom; (555 735) right top

change the four number to crop the image. You can check it in GSview.

What if EPS images make the file too big?

use GSview (Ghostscript)

file -> convert -> [personal taste. 3D EPS from Mathematica: png16+Resolution300. chart from Calc: png256+Resolution300]

cut png/JPG? ? use IrfanView+PlugIns

edit -> Auto crop borders

Written by Jingjing Wang

10/24/2011 at 9:17 pm

Posted in LyX

Data Source

leave a comment »

Harvard IQSS Dataverse Network: the world’s largest collection of social science research data

CA maps: CA-ATLAS

Written by Jingjing Wang

09/22/2011 at 3:07 pm

Posted in Data

Windows Tips

leave a comment »

windows + R: Run (cmd for “Command Prompt”)

windows + D: Desktop

windows + F: Find

Alt + Tab: switch between running programs

Written by Jingjing Wang

09/22/2011 at 3:00 pm

Posted in OS