Repository: Qiskit/qiskit

Issue:
circuit.draw(output = 'latex') truncates circuit unexpectedly
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

- **Qiskit Terra version**: 0.7.0
- **Python version**: 3.6.5
- **Operating system**: Windows 10

### What is the current behavior?
test.draw(output = 'latex')

![image](https://user-images.githubusercontent.com/38588126/51922343-d7059c80-23b6-11e9-9125-ebc7ad7c573b.png)


### Steps to reproduce the problem

```
numq = 6
q = QuantumRegister(numq, name='qn')
c = ClassicalRegister(numq, name='cn')
test = QuantumCircuit(q,c)
for i in range(numq):
    test.h(q[i])
    test.barrier(q[i])
    
for i in range(numq-1):
    test.h(q[i+1])
    test.cx(q[i],q[i+1])
    test.h(q[i+1])
    test.barrier(q[i+1])
```



### What is the expected behavior?
test.draw(output = 'mpl')

![image](https://user-images.githubusercontent.com/38588126/51922373-ea186c80-23b6-11e9-9b67-54d63e358ef2.png)


### Suggested solutions

possibly a issue with latex or windows, might not be terra related




Discussion:
This is definitely a bug, and almost certainly related to the barrier implementation in the `qiskit/tools/visualization/_latex` module. I think what's going on (but I have yet to confirm this) is that the index where we insert the barriers is incorrect and ends up overwriting the hadamard gates instead of inserting a barrier after (which is the intended behavior). You can at least confirm that the barriers are what's at fault in your example by calling `test.draw(output='latex', plot_barriers=False)` which will show all the gates as expected.
Yep, I just confirmed what I suspected is the cause of this, is indeed the underlying cause of this bug. For example, the list of latex commands used to draw the circuit (for the qn_5 qubit) goes from being:

` ['\\lstick{qn_{5}: \\ket{0}}', '\\qw \\barrier{0}',  '\\gate{H}',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw', '\\qw',  '\\qw', '\\qw', '\\qw', '\\qw', '\\targ', '\\gate{H}', '\\qw', '\\qw']`

prior to drawing the barrier, and becomes:

` ['\\lstick{qn_{5}: \\ket{0}}',  '\\qw \\barrier{0}',  '\\gate{H}',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  \\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\targ',  '\\qw \\barrier{0}',  '\\qw',  '\\qw']`

after drawing the barrier. So the index we use to insert the barrier into this case is incorrect. I'm not sure why this is the case but with a simple reproduce case it'll be just a matter of debugging the cause. (this is just another example of why I think we should rewrite this module, since it's a rats nest) The expected output should be:


` ['\\lstick{qn_{5}: \\ket{0}}',  '\\qw \\barrier{0}',  '\\gate{H}',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  \\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\qw',  '\\targ', '\gate{H}',  '\\qw \\barrier{0}',  '\\qw']`

Resolving patch:
diff --git a/qiskit/tools/visualization/_latex.py b/qiskit/tools/visualization/_latex.py
--- a/qiskit/tools/visualization/_latex.py
+++ b/qiskit/tools/visualization/_latex.py
@@ -423,13 +423,13 @@ def _get_image_depth(self, aliases=None):
                         qarglist = map(lambda x: aliases[x], qarglist)
                     start = self.img_regs[start_bit]
                     span = len(op['qargs']) - 1
-                    for i in range(start, span + 1):
+                    for i in range(start, start + span + 1):
                         if is_occupied[i] is False:
                             is_occupied[i] = True
                         else:
                             columns += 1
                             is_occupied = [False] * self.img_width
-                            for j in range(start, span + 1):
+                            for j in range(start, start + span + 1):
                                 is_occupied[j] = True
                             break
                     # update current column width
@@ -936,13 +936,13 @@ def _build_latex_array(self, aliases=None):
                         qarglist = map(lambda x: aliases[x], qarglist)
                     start = self.img_regs[start_bit]
                     span = len(op['qargs']) - 1
-                    for i in range(start, span + 1):
+                    for i in range(start, start + span + 1):
                         if is_occupied[i] is False:
                             is_occupied[i] = True
                         else:
                             columns += 1
                             is_occupied = [False] * self.img_width
-                            for j in range(start, span + 1):
+                            for j in range(start, start + span + 1):
                                 is_occupied[j] = True
                             break
                     self._latex[start][columns] = "\\qw \\barrier{" + str(


Test patch: