“Pretty” understates what formatting is really about. Making an object more pleasing to the eye is nice, but the point is really about communicating better with the reader of the output. Formatting the output appropriately can improve that process. In previous posts, I have written about ways to format pivot tables: the […]
“Pretty” understates what formatting is really about. Making an object more pleasing to the eye is nice, but the point is really about communicating better with the reader of the output. Formatting the output appropriately can improve that process. In previous posts, I have written about ways to format pivot tables: the most important textual output from SPSS. In this post, I will discuss a way to create and format text blocks.
Inexplicably, SPSS has never had a good, built-in, production way of writing a block of text to the Viewer. Syntax can be echoed to a log file, including the COMMENT command, but logs are full of other things and don’t work well for communicating with readers, especially if they are not conversant with SPSS syntax. You can, of course, interactively insert a text block in the Viewer, but that’s a non-starter for production work. There are scripting apis for text blocks, too, but they require a knowledge of script programming.
For this reason, I created the TEXT extension command. (Recall that extension commands are user-defined commands that can be installed and are then available like regular commands.) TEXT takes a list of literals and creates a new text block object in the Viewer with each literal as a line. You can control the outline text and title information for the block as well. That works with Version 16 or later. With Version 17, though, you can present this text better by adding formatting.
To do that, all you have to do is to create the text as html or rtf and feed it to the TEXT command. Here’s an example.
TEXT "<html>The following table is "
"<font size=6><b><i>confidential</i></b> </font>.<br> "
"It must not be shared outside the company</html>"
/OUTLINE HEADING="Notice".
Here’s what the resulting text block looks like.
The principle is that if the text looks like html or rtf, it will be treated as formatted text; otherwise, it will be considered to be plain. For html, this means starting with <html> in lower case. This example specifies the “confidential” text to be size 6 (html sizes go from 1 to 7), bold, and italic. It also inserts a line break. A web search on html will produce many pages to explain the html markup language. One good introduction is Introduction to Html.
The text in this example is split into several strings. Because it is formatted, the strings are joined without any added spaces or line breaks. Html directives such as <br> would be used to break lines if needed.
Basic html is simple to write, and there are plenty of free html editors you can use, or just use a plain text editor, but keep the html simple. Only basic formatting is supported. Don’t use Microsoft Word to create the html: it creates very complex and hard-to-process html. For rtf, you might use a program like WordPad to create the rtf and then copy it into your syntax, but rtf is quite complex, and it makes your syntax very hard to read, so I recommend sticking with html.
The TEXT extension command can be downloaded from SPSS Developer Central. It requires the Python programmability Plug-In. The same download works for Versions 16 and 17, but with Version 16, only plain text can be inserted.
Formatting the Outline and Titles
Rather to my surprise, I learned that some users spend a lot of time structuring and formatting the Viewer outline and headings and titles. I created the extension command SPSSINC MODIFY OUTPUT to eliminate the complexity and drudgery in that work.
It’s not well known, but title and heading objects in the Viewer can be formatted with html or rtf in the same way as I just described for text blocks: if the text looks like html or rtf, it is treated that way. The text generated by SPSS procedures, though, doesn’t include such formatting. SPSSINC MODIFY OUTPUT can upgrade the text, and it can do other things to output objects. More on that in my next post.