Render frames and the LaTeX listings package

From Scribus Wiki
Revision as of 16:00, 7 January 2017 by Gpittman (talk | contribs)
Jump to: navigation, search

A recent question on the Scribus mail list related to a desire to make some posters with Scribus which depict programming code, and to be able to show syntax highlighting. Various solutions were proposed, but the original poster wanted to try to use LaTeX in a Render frame to accomplish this, and met obstacles he could not sort out.

Finally, with some additional advice, and some hours of working this, this was sorted out, using the LaTeX package listings. It's also worth adding that in order to show syntax in color you also must use the color package. What follows here is an explanation of the process, using a Python script as an example. Much of the time was spent using LaTeX outside of Scribus in order to get the syntax right from a LaTeX perspective, but also to be able to compare the end result with the Scribus/Render frame method.

Here is the script we'll work with:

#!/usr/bin/env python
# -*- coding: utf-8  -*-

# ****************************************************************************
#  This program is free software; you can redistribute it and/or modify 
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ****************************************************************************


"""

© 2017 Gregory Pittman

bcaption.py

Creates a text frame (caption) below one or more selected frames.

"""

try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx, fy+fheight, fwidth, 24)
    
scribus.setUnit(pageunits)

scribus.setRedraw(True)

Something I found out quickly was that listings had a lot of trouble with the extended comment with the copyright information, and also the USAGE section of the script. Since these were hardly desired, they were deleted. After a great deal of trial and error, fussing and fuming, here is the end result that worked pretty well to my liking:

\documentclass{article}
\usepackage{listings, color, tgheros}
\setlength{\textwidth}{18cm}
  \setlength{\oddsidemargin}{-1cm}
  \setlength{\topmargin}{-1.5cm}

\begin{document}
\lstset{
  basicstyle=\sffamily,
  keywordstyle=\color{red},
  commentstyle=\color[rgb]{0,0,0.9},
  showstringspaces=false,
  }
\begin{lstlisting}[language=Python,showspaces=0]
#!/usr/bin/env python
# bcaption.py
  
try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame
          selected", scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx, fy+fheight, fwidth, 24)
    
scribus.setUnit(pageunits)

scribus.setRedraw(True)
\end{lstlisting}

\end{document}