Formatting source listings with listings & framed packages - formatting

I currently have a problem, that listings package cannot spread source files across multiple pages. In the doc is written, that the "framed" package should be used for various formatting option. Unfortunately I did not find any docs for the "framed" package. My current source formatting looks like this for C# sources:
Source Formatting http://www.free.image.hosting.net/uploads/88987a1ef4.png
Unfortunately the image service no longer exists and I can't find that image, since the post was posted more than 5 years ago. What I remember is that the formatted source code part, which should be visible on the next page, was just truncated and did not show up at all.
My formatting for "listings" package is:
\newcommand{\sourceFormatterCSharp}
{
\lstset
{ language=[Sharp]C
, captionpos=b
%, frame=lines
, morekeywords={var, get, set}
, basicstyle=\footnotesize\ttfamily
, keywordstyle=\color{blue}
, commentstyle=\color{darkgreen}
, stringstyle=\color{darkred}
, backgroundcolor=\color{lightgrey}
, numbers=left
, numberstyle=\scriptsize
, stepnumber=2
, numbersep=5pt
, breaklines=true
, tabsize=2
, showstringspaces=false
, emph={double, bool, int, unsigned, char, true, false, void, get, set}
, emphstyle=\color{blue}
, emph={Assert, Test}
, emphstyle=\color{red}
, emph={[2]\#using, \#define, \#ifdef, \#endif}
, emphstyle={[2]\color{blue}}
, frame=shadowbox
, rulesepcolor=\color{grey}
, lineskip={-1.5pt} % single line spacing
}
}
% first optional param is placement
% param1 file name without extension
% param2 chapter number, e.g. 1 or 2 ...
% param3 caption to use
\newcommand{\embedCSharp}[4][htbp]
{
\sourceFormatterCSharp
\includeListing{#1}{#4}{#3:#2}{#3/#2.cs}
}
Can anybody help me achieving similar looking results using "framed" package or any other for my source to look like this but be distributable across pages? An example how to embed a listing in the frame would not satisfy, since I was so far myself.

The listings package already supports splitting code across pages; see example below (sorry about the long listing). Note that you cannot have a float that breaks across pages, so you'll need to use the caption package (for example) to insert a caption at the beginning of the lstlisting environment.
\documentclass{article}
\usepackage[a5paper,landscape]{geometry}
\usepackage{xcolor,listings}
\begin{document}
\definecolor{lightgrey}{gray}{0.8}
\lstset
{
captionpos=b
, backgroundcolor=\color{lightgrey}
, numbers=left
, numberstyle=\scriptsize
, stepnumber=2
, numbersep=5pt
, frame=shadowbox
, rulesepcolor=\color{gray}
}
\begin{lstlisting}
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
\end{lstlisting}
\end{document}

The framed documentation is within the .sty file itself. Just use it like this:
\documentclass{article}
\usepackage{framed,lipsum}
\begin{document}
\begin{framed}
\lipsum[1-10]
\end{framed}
\end{document}
From the docs, you can also use:
framed -- ordinary frame box (\fbox) with edge at margin
shaded -- shaded background (\colorbox) bleeding into margin
snugshade -- similar
leftbar -- thick vertical line in left margin
Putting your listings instead of lipsum in the example above will allow multiple pages of code with a frame around it all; you won't be able to get identical output to listings, but should be able to tweak things to get things looking okay.

Related

Code folding in RStudio: Creating hierarchy in the code

I'm writing R scripts in RStudio and I use the code folding a lot. I found that you can see the hierarchy of the folding by pressing cmd + shift + O. This is super helpful.
# to my dear love ---------------------------------------------------------
2+2
# yo man ====
x.2 = function (x) {x+2}
### I do love potatoes ####
See the result by pressing cmd + shift + O.
I don't understand how this is working because when I write the code below, I can create a subsection without text but not when there is text in it (using # ==== but not # yo man ====).
# to my dear love ---------------------------------------------------------
2+2
# ====
# yo man ====
### I do love potatoes ####
x.2 = function (x) {x+2}
data = "here is some data"
See the result by pressing cmd + shift + O.
You can see that under # to my dear love --------------------------------------------------------- everything under is shifted to the right! This is cool!
The question is thus, how could it be possible to create a hierarchy of sections that include text in it?
Is it a peculiar package or Emac that is doing this? How can I create subsections, with text, and see the hierarchy in the cmd + shift + O box?
How can I down shift a section (going to a higher section (say section 2) to a lower section (section 1), by decreasing the visual hierarchy in the right box?
As per Chris's answer subheaders within functions
RStudio Code Folding hierarchy only works within function definitions and if-else structures. For example:
# Section 1 ----
a <- 1
testfunct1 <- function () {
# sect in function=====
b <- 2
c <- 3
}
# Section 2 #####
d <- 4
# Section 3 =======
e <- 5
testfunct2 <- function () {
# sect in function 2 =====
f <- 6
testsubfunct2_1 <- function () {
# sect in subfunction 2_1 -----
if (a == 1) {
# section in if ----
g < 7
} else {
# section in else ----
h = 8
}
}
}
# Section 4 ####
j <- 9
Produces this outline:
I don't know why the if-else section labels do not line up.
Just discovered I could use various special characters across the top of my keyboard in combination with hyphens to produce a hierarchical look to the code section ToC. I chose the asterisk for this example, but you can use anything from the special character keys across the top to produce this look.

Load multiple Excel sheets using For loop with QlikView

I want to load data from two different Excel files, and use them in the same table in QlikView.
I have two files (DIVISION.xls and REGION.xls), and I'm using the following code:
let tt = 'DIVISION$' and 'REGION$';
FOR Each db_schema in 'DIVISION.xls','REGION.xls'
FOR Each v_db in $(tt)
div_reg_table:
LOAD *
FROM $(db_schema)
(biff, embedded labels, table is $(v_db));
NEXT
NEXT
This code works fine, and does not show any error, but I don't get any data, and I don't see my new table (div_reg_table).
Can you help me?
The main reason that your code does not load any data is due to the form of the tt variable.
When your inner loop executes, it evaluates tt (denoted by $(tt)), which then results in the evaluation of:
'DIVISION$' AND 'REGION$'
Which results in null, since these are just two strings.
If you change your statement slightly, from LET to SET and remove the AND, then the inner loop will work. For example:
SET tt = 'DIVISION$', 'REGION$';
However, this also now means that your inner loop will be executed for each value in tt for both workbooks. This means that unless you have a REGION sheet in your DIVISION workbook, then the load will fail.
To avoid this, you may wish to restructure your script slightly so that you have a control table which details the files and tables to load. An example I prepared is shown below:
FileList:
LOAD * INLINE [
FileName, TableName
REGION.XLS, REGION$
DIVISION.XLS, DIVISION$
];
FOR i = 0 TO NoOfRows('FileList') - 1
LET FileName = peek('FileName', i, 'FileList');
LET TableName = peek('TableName', i, 'FileList');
div_reg_table:
LOAD
*
FROM '$(FileName)'
(biff, embedded labels, table is '$(TableName)');
NEXT
If you have just two files DIVISION.XLS and REGION.XLS then it may be worth just using two separate loads one after the other, and removing the for loop entirely. For example:
div_reg_table:
LOAD
*
FROM [DIVISION.XLS]
(biff, embedded labels, table is DIVISION$)
WHERE DivisionName = 'AA';
LOAD
*
FROM [REGION.XLS]
(biff, embedded labels, table is REGION$)
WHERE RegionName = 'BB';
Don't you need to make the noofrows('Filelist') test be 1 less than the answer.
noofrows('filelist') will evaluate to 2 so you will get a loop step for 0,1 and 2. So 2 will return a null which will cause it to fail.
I did it like this:
FileList:
LOAD * INLINE [
FileName, TableName
REGION.XLS, REGION
DIVISION.XLS, DIVISION
];
let vNo= NoOfRows('FileList')-1;
FOR i = 0 TO $(vNo)
LET FileName = peek('FileName', i, 'FileList');
LET TableName = peek('TableName', i, 'FileList');
div_reg_table:
LOAD
*,
$(i) as I,
FileBaseName() as SOURCE
FROM '$(FileName)'
(biff, embedded labels, table is '$(TableName)');
NEXT

Apache PDFBox Remove Spaces between characters

We are using PDFBox to extract text from PDF's.
Some PDF's text can't be extract correctly.
The following image shows a part from the PDF as image:
After text extraction we get the following text:
3, 8 5 EU R 1 Netto 38,50 EUR 4,00
(Spaces are added between ',' and '8')
Here is our code:
PDDocument pdf = PDDocument.load(reuseableInputStream);
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setSortByPosition(true);
String text = pdfStripper.getText(pdf);
We tried to play with the PDFTextStripper attributes 'AverageCharTolerance' and 'SpacingTolerance' with no positive effect.
The alternative libary 'iText' extract the text correctly without spaces between the characters. But we can't use it because of license problems.
Any ideas? Thank you.
EDIT: We are using version 1.8.9. We tried also the snapshot version 2.0.0 with no effect.
The cause
Inspecting the file provided by the OP it turns out that the issue is caused by extra spaces actually being there! There are multiple strings drawn from the same starting position; at every position at most one of those strings has a non-space character. Thus, the PDF viewer output looks good, but PDFBox as text extractor tries to make use of all characters found including those extra space characters.
The behavior can be reproduced using a PDF with this content stream with F0 being Courier:
BT
/F0 9 Tf
100 500 Td
( 2 Netto 5,00 EUR 3,00) Tj
0 0 Td
( 2882892 ENERGIZE LR6 Industrial 2,50 EUR 1) Tj
ET
In a PDF viewer this looks like this:
Copy & paste from Adobe Reader results in
2 2 8 8 2 8 9 2 E N E R G I Z E L R 6 I n d u s t r i a l 2 , 5 0 E U R 1 Netto 5,00 EUR 3,00
Regular extraction using PDFBox results in
2 2 8 8 2 89 2 E N E RG IZ E L R 6 I n du s t ri a l 2 ,5 0 EU R 1 Netto 5,00 EUR 3,00
Thus, not only PDFBox has problems here, these two outputs look different but the extra spaces are a problem either way.
I would propose telling the producer of those PDFs that they are difficult to post-process, even for widely-used software like Adobe Reader.
A work-around
To extract something sensible from this we have to somehow ignore the (actually existing!) extra spaces. As there is no way to ad hoc know which spaces can be used later on and which not, we simply remove all and hope PDFBox adds spaces where necessary:
String extractNoSpaces(PDDocument document) throws IOException
{
PDFTextStripper stripper = new PDFTextStripper()
{
#Override
protected void processTextPosition(TextPosition text)
{
String character = text.getCharacter();
if (character != null && character.trim().length() != 0)
super.processTextPosition(text);
}
};
stripper.setSortByPosition(true);
return stripper.getText(document);
}
(ExtractWithoutExtraSpaces.java)
Using this method with the test document we get:
2 2882892 ENERGIZE LR6 Industrial 2,50 EUR 1 Netto 5,00 EUR 3,00
Different text extractors
The alternative libary 'iText' extract the text correctly without spaces between the characters
This is due to iText extracting text string by string, not character by character. This procedure has its own perils but in this case results in something more usable out-of-the-box.
On newer versions of PDFBox the workaround doesn't work.
But you can fix the problem space and achieve the same result just setting your PDFTextStripper like that:
PDFTextStripper strippet = new PDFTextStripper();
stripper.setWordSeparator("");

SQL Server 2008 Split 1 column into 3 (Shipping dimensions)

I have a table with with one column for product dimensions.
example rows:
16" L x 22" W x 6" H
22.5" L x 12" W x 9" H
I am trying to get the length, width, and height into separate columns. I have to use SQL because this is being used in a software integration that only accepts SQL statements. I am thinking I have to go the route of regex.
SQL Statement to get the data so far
SELECT TOP 10
[ID]
,substring([SHIP_DIMENSIONS],PATINDEX('%[0-9]\"%',[SHIP_DIMENSIONS]),2) as Length
,substring([SHIP_DIMENSIONS],PATINDEX('%[0-9]\"%',[SHIP_DIMENSIONS]),2) as Width
,substring([SHIP_DIMENSIONS],PATINDEX('%[0-9]*\"%',[SHIP_DIMENSIONS]),2) as Height
FROM [PART]
I need the output to be
Length | Width | Height
16 | 22 | 6
22.5 | 12 | 9
Any suggestions would be greatly appreciated.
One way to do it is as follows:
select
left(dim, charindex('" L', dim)-1) as [Length]
, substring(dim, charindex('" L', dim)+6, charindex('" W', dim)-charindex('" L x ', dim) - 6) as [Width]
, substring(dim, charindex('" W', dim)+6, charindex('" H', dim)-charindex('" W x ', dim) - 6) as [Height]
from test
The idea is to look for the markers that you have in your text, and use them to parcel out the string into substrings. This approach is very rigid, in that it assumes that the pattern shown in your example is followed precisely in all your records, i.e. all the markers are present, along with the spaces. There is also an implicit assumption that all dimensions are in inches. What can vary is the width of the columns.
Demo.
Note: I am assuming that you are dealing with a legacy database, so there is no way to do the right thing (which is to separate out the dimensions into separate columns).

gnuplot, how to label only certain points?

I'm using the following gnuplot commands to create a plot:
#!/bin/bash
gnuplot << 'EOF'
set term postscript portrait color enhanced
set output 'out.ps'
plot 'data_file' u 3:2 w points , '' u 3:2:($4!=-3.60 ? $1:'aaa') w labels
EOF
where data_file looks like this:
O4 -1.20 -0.33 -5.20
O9.5 -1.10 -0.30 -3.60
B0 -1.08 -0.30 -3.25
B0.5 -1.00 -0.28 -2.60
B1.5 -0.90 -0.25 -2.10
B2.5 -0.80 -0.22 -1.50
B3 -0.69 -0.20 -1.10
I want gnuplot to label all points with the strings found in column 1, except the one where column 4 is equal to -3.60 in which case I want the aaa string. What I'm getting is that the $4=-3.60 data point is being labeled correctly as aaa, but the rest are not being labeled at all.
Update: gnuplot has no problem showing numbers as labels using the conditional statement, ie: any column but 1 is correctly displayed as a label for each point respecting the conditions imposed. That is, this line displays column 2 (numbres) as point labels respecting the conditional statement:
plot 'data_file' u 3:2 w points , '' u 3:2:($4!=-3.60 ? $2:'aaa') w labels
Update 2: It also has no problem in plotting column 1 as point labels if I plot it as a whole, ie not using a conditional statement. That is, this line plots correctly all the point labels in column 1 (strings):
plot 'data_file' u 3:2 w points , '' u 3:2:1 w labels
So clearly the problem is in using the conditional statement together with the strings column. Any of these used separately works just fine.
In a more clean way maybe, this should work. It seems label can't display a computed number if it isn't turned in a string.
#!/bin/bash
gnuplot << 'EOF'
set term postscript portrait color enhanced
set output 'out.ps'
plot 'data_file' u 3:2 w points , '' u 3:2:($4!=-3.60 ? sprintf("%d",$1):'aaa') w labels
EOF
Is this what you want?
#!/bin/bash
gnuplot << 'EOF'
set term postscript portrait color enhanced
set output 'out.ps'
plot 'data_file' u 3:2 w points , \
'' u (($4 == -3.60)? 1/0 : $3):2:1 w labels
EOF
All I do here is set (x) points where the column 4 equals -3.6 to NaN (1/0). Since gnuplot ignores those points, life is good. I think the problem with your script is that you were filtering a column where gnuplot expects string input -- although I haven't played around with it enough to verify that. I just switched the filter to a column where gnuplot expects numbers (the x position) and it works just fine.